#Automatically Determine Unallocated Space And Expand Drive C In PowerShell After Expanding Drive In Hyper-V
#Code to automatically determine unallocated space and expand boot drive
#NOTE: I am not responsible if you muck everything up. I am working in a lab under optimal conditions. Always backup your critical data before messing with your hard drives and virtual machines. BACKUP BACKUP BACKUP!!! if you are doing this in a production environment. At least create a checkpoint in Hyper-V so you can go back if needed. I am not responsible for random code snippets I wrote or posted here. You choose what to run on your computers and I am not part of that decision or any undesired consequences. Ok then, moving on…
$Drive2Expand = 'C' to whatever other drive you wish to expand but be aware that I am running
Get-Disk with the
IsBoot parameter equal to
Yes. If you change
$Drive2Expand to a non-boot drive these commands with fail.
#Just the code:
$Drive2Expand='C' #Change to the NTFS drive letter you want to expand $VirtDiskNum=(Get-Disk -FriendlyName 'Msft Virtual Disk'|? IsBoot -eq Yes) #Get disk number of default hyper-v boot drive but change if expanding a secondary or non-boot drive $PartitionNum=(Get-Partition -DriveLetter $Drive2Expand) #Get the partition number for drive c $PartSize=(Get-PartitionSupportedSize -DiskNumber $VirtDiskNum.number -PartitionNumber $PartitionNum.PartitionNumber) #Get partition info for drive c Resize-Partition -PartitionNumber $PartitionNum.PartitionNumber -Size $PartSize.SizeMax -DiskNumber $VirtDiskNum.number #Expand drive C using all unallocated space available
#The longer story…
#This used to be done with
diskpart but PowerShell can get the job done without launching an application and is much more easily scripted.
#Run Get-Disk to get the disk number
? is an alias to the
where command. You can change
IsBoot to any parameter or value listed with the
Format-List command or
fl in the code I type. I am basing this on working with virtual hard drives in Hyper-V but these command can be modified to address the same issues with physical drives.
Get-Disk|ft -a #If you have only one virtual disk installed then this command is fine. Get-Disk -FriendlyName 'Msft Virtual Disk'|? IsBoot -eq Yes|fl #Get a list of the available parameters on an automatically created default boot drive
-DriveLetter C to get drive
C configuration information
PartitionNumber in the command output as that will be needed for the rest of the commands. Change
-DriveLetter to whatever drive you wish to expand.
Get-Partition -DriveLetter C
#Another option is to get only the
PartitionNumber value instead of the standard output.
Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber $PartitionNum.PartitionNumber
#Check minimum and maximum supported sizes for the partition
Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber 4
Resize-Partition to use the maximum size available to expand –
#I just copied and pasted the
SizeMax number from above to the command below. I can’t screenshot this command at the moment since my drive is fully expanded but I update with screenshot soon.
Resize-Partition -PartitionNumber 4 -Size 2209358000 -DiskNumber 0 #Expand drive C using all unallocated space available
#Get free space on drive
#The following commands are to get the free space on drive
C and display it in a friendly manor. This code is just for fun. It is not really needed. Is just to show how to manipulate number results in PowerShell by converting the free space on drive
C to a easily readable format. Many of the principles below can be applied to the results of the above commands.
$Free = (Get-PSDrive C) #Set drive information to variable $Free = ($Free.free/1GB) #Get free space and divide by 1 gigabyte $Free = ([math]::Round($Free,2)) #Round free space to two decimal places Write-Host $Free'/GB Free on Drive C:\' #Display free space is an easily readable format