LoginSignup
1
2

More than 5 years have passed since last update.

PowerShellだけでパーティションを拡張する

Last updated at Posted at 2019-02-01

目的

ディスクに1個だけパーティションがあって、
それを目一杯拡張したい!

こんな感じのパーティションを...
image.png

PowerShellだけでこのようにするのが目的です
image.png

条件など

以下の環境で動作確認しました。
Windows Server 2016
PowerShell5.1

ソース

ドライブレターを指定して拡張するようにしてあります。
エラー処理が入ってないので適宜追加しましょう。
パーティションが1個でなくても末尾であれば拡張できます。

DiskExpand.ps1
param (
  [parameter(mandatory = $true, Position = 0)]
  [string]
  $DriveLetter
)

$Partition = Get-Partition | Where-Object -Property 'DriveLetter' -Value $DriveLetter -EQ
if ($Partition) {
    $PartitionSupportedSize = $Partition | Get-PartitionSupportedSize
    if ($PartitionSupportedSize.SizeMax -lt $Partition.Size) {
        Resize-Partition -DiskNumber $Partition.DiskNumber -PartitionNumber $Partition.PartitionNumber -Size $PartitionSupportedSize.SizeMax
    }
}

使い方

この例ではEドライブを拡張します。


DiskExpand.ps1 -DriveLetter 'E'

参考

1
2
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
2