-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows-resize_partition.ps1
61 lines (50 loc) · 2.05 KB
/
windows-resize_partition.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<#
.SYNOPSIS
Script resizes a partition, if it has been expanded with 1GB or more - sets to MAX size
.DESCRIPTION
Script resizes a partition, if it has been expanded with 1GB or more - sets to MAX size
.INPUTS
N/A
.NOTES
Author: Anders Mikkelsen
Creation Date: 2021-12-23
GitHub: https://github.com/amikkelsendk
Twitter: @AMikkelsenDK
Tested on:
- Windows Server 2019
Requirements:
Must run with administrative priviliges
Credits to:
https://www.controlup.com/script-library-posts/extend-a-logical-disk-to-maximum-partition-size-for-that-volume/
#>
# Rescan Storage/Disks
Update-HostStorageCache
# Get list of disks
$arrDisks = Get-Disk | Where-object{$_.OperationalStatus -eq "Online" -and $_.PartitionStyle -eq "GPT"}
Foreach ( $objDisk In $arrDisks ) {
Write-Host "Disk: $($objDisk.Number)"
$arrPartitions = $objDisk | Get-Partition
If ( $arrPartitions.Count -eq 1 ) {
Foreach ( $objPartition In $arrPartitions ) {
$thisPartitionDriveLetter = $objPartition.DriveLetter
$thisPartitionDiskSize = $objPartition.Size
#If ( $thisPartitionDriveLetter ) {
If ( ( $thisPartitionDriveLetter ) -and ( $thisPartitionDriveLetter -ne "C" ) ) {
Write-Host " DriveLetter: $thisPartitionDriveLetter"
Write-Host " Current Size: $thisPartitionDiskSize"
$thisPartitionDiskMaxSize = ( Get-PartitionSupportedSize -DriveLetter $thisPartitionDriveLetter ).SizeMax
# Only if new size is 1GB larger
$thisSizeGB = [math]::Round( $thisPartitionDiskSize/1024/1024/1024, 2 )
$thisSizeMaxGB = [math]::Round( $thisPartitionDiskMaxSize/1024/1024/1024, 2 )
If ( $thisSizeGB -lt $thisSizeMaxGB ) {
# Resize partition to MAX
Write-Host " New size: $thisPartitionDiskMaxSize"
Resize-Partition -DriveLetter $thisPartitionDriveLetter -Size $thisPartitionDiskMaxSize -Confirm:$false
}
}
}
}
Else{
Write-Host " !! has 0 or more than 1 partition !!"
}
}