1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cドライブのパーティションを 半分に分割するバッチ

Posted at

Cドライブを分割してパーティションをCドライブとDドライブに分ける際、
ちょうど半分に分けたいといったときに使えるバッチです。

c_harf.bat
@echo off

rem 管理者権限強制実行
whoami /priv | find "SeDebugPrivilege" > nul
if %errorlevel% neq 0 (
 @powershell start-process %~0 -verb runas
 exit
)

powershell -ExecutionPolicy bypass -command  "get-partition -driveletter C | resize-partition -size ((GWMI win32_logicaldisk | where-object {$_.deviceid -eq \"C:\"}).size /2)"

Powershellにて現在のCドライブの容量を取得し、半分の値を計算、
Cドライブを半分に縮小しています。
パーティション分割には管理者権限が必要なので、
バッチの最初の部分で必ず管理者権限で実行するようにしています。

ドライブ文字を変更すれば別ドライブでも対応可となります。

最後の /2*0.5 と書いてもOKです。この値を変えることで、好きなサイズに変更することができます。
50%なので0.5となっていますので、70%に縮小の場合は *0.7 と記載してください。
また、空き領域がある場合は拡張も可能ですので、
110%にしたい場合は *1.1 と記載してください。
ただし、縮小できる、拡張できる最大値を超えてはできないので注意が必要です。

Powershellで動かしていますので、ps1形式でも書くことができ、
ps1形式の場合は以下のようになります。

harf.ps1
#管理者権限強制実行
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) { Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs; exit }

get-partition -driveletter C | resize-partition -size ((GWMI win32_logicaldisk | where-object {$_.deviceid -eq \"C:\"}).size /2)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?