Created
June 17, 2024 18:09
-
-
Save RobBiddle/3c92289a639b9f4fb4dbda3145af5a97 to your computer and use it in GitHub Desktop.
Resizes all partitions on a disk to the maximum size available on the disk. Windows OS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Resizes all partitions on a disk to the maximum size available on the disk. | |
.DESCRIPTION | |
This script will resize all partitions on a disk to the maximum size available on the disk. | |
This is useful when you have a disk that has been expanded in the hypervisor and you want to | |
expand the partitions to use the new space. The script supports use of -WhatIf. | |
.NOTES | |
Author: Robert D. Biddle | |
.LINK | |
.EXAMPLE | |
Expand-AllVolumesToMaxSize.ps1 | |
This will expand all partitions on the local computer to the maximum size available on the disk. | |
.EXAMPLE | |
Expand-AllVolumesToMaxSize.ps1 -ComputerName Server1 | |
This will expand all partitions on Server1 to the maximum size available on the disk. | |
.EXAMPLE | |
Expand-AllVolumesToMaxSize.ps1 -ComputerName Server1,Server2 | |
This will expand all partitions on Server1 and Server2 to the maximum size available on the disk. | |
.EXAMPLE | |
Get-Content .\Servers.txt | Expand-AllVolumesToMaxSize.ps1 | |
This will expand all partitions on the servers listed in Servers.txt to the maximum size available on the disk. | |
.EXAMPLE | |
Get-Content .\Servers.txt | Expand-AllVolumesToMaxSize.ps1 -WhatIf | |
This will show what would happen if you ran the command in the previous example. | |
#> | |
[CmdletBinding(SupportsShouldProcess)] | |
param ( | |
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[Alias('CN', 'Computer', 'Server', 'Servers')] | |
[string[]]$ComputerName = @($env:COMPUTERNAME) | |
) | |
begin { | |
$ErrorActionPreference = 'Stop' | |
} | |
process { | |
foreach ($computer in $ComputerName) { | |
try { | |
$volumes = Get-Volume -CimSession $computer -ErrorAction Stop | Where-Object { $_.DriveLetter -ne $null } | |
foreach ($volume in $volumes) { | |
Write-Output "Checking Volume: $($volume.DriveLetter) on Computer: $computer" | |
$partition = Get-Partition -CimSession $computer -Volume $volume -ErrorAction Stop | |
if ($null -ne $partition) { | |
Write-Output "Checking Partition: $($partition.DiskNumber) on Volume: $($volume.DriveLetter) on Computer: $computer" | |
Update-Disk -CimSession $computer -Number $partition.DiskNumber | |
$diskSize = Get-PartitionSupportedSize -CimSession $computer -DiskNumber $partition.DiskNumber -PartitionNumber $partition.PartitionNumber | |
Write-Output "Disk Size: $($diskSize.SizeMax) Partition Size: $($partition.Size)" | |
if ($diskSize.SizeMax -gt $partition.Size) { | |
if ($PSCmdlet.ShouldProcess) { | |
Write-Output "Resizing Drive $($volume.DriveLetter): $($partition.DiskNumber) from: $($partition.Size) to: $($diskSize.SizeMax)" | |
Resize-Partition -CimSession $computer -DiskNumber $partition.DiskNumber -PartitionNumber $partition.PartitionNumber -Size $diskSize.SizeMax -Confirm:$false | |
Update-Disk -CimSession $computer -Number $partition.DiskNumber | |
} | |
else { | |
Write-Output "Resizing Drive $($volume.DriveLetter): $($partition.DiskNumber) from: $($partition.Size) to: $($diskSize.SizeMax)" | |
Resize-Partition -CimSession $computer -DiskNumber $partition.DiskNumber -PartitionNumber $partition.PartitionNumber -Size $diskSize.SizeMax -Confirm:$false | |
Update-Disk -CimSession $computer -Number $partition.DiskNumber | |
} | |
} | |
else { | |
Write-Output "Drive $($volume.DriveLetter): $($partition.DiskNumber) already at max size of $($diskSize.SizeMax)" | |
} | |
} | |
} | |
} | |
catch { | |
Write-Error $Error[0] | |
} | |
} | |
} | |
end { | |
$ErrorActionPreference = 'Continue' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment