If you are running a Windows virtual machine, you can quickly run out of space on your system drive, thanks to a great feature called “Windows Update” that can fill up your drive with caches of all the updates you have installed. So, how do you add more space to your hard drive without disrupting your system and having to rebuild it from scratch? The solution is much simpler than you think!
Typically, for a modern day Windows virtual machine, I allocate about 40 GB of disk space for the system (C:) drive. This is of course, more than sufficient to hold all of the applications and tools that I tend to install and use. However, over a period of time, the Windows system tends to accumulate a lot of updates. Updates for the Windows OS, updates for other software and tools and drivers. Trying to be helpful for the scenario (quite rare for a lot of us) who track down an errant system behavior to an installed update and uninstall that update specifically, the system maintains copious caches of all the updates your system has installed. This means that slowly, your system drive can lose all of its free space to such cached files that you will never need!
Yup… Disk Cleanup can usually catch and delete such cached up files if you checkmark the “Windows Update Cleanup” option after the disk scan phase is complete (see below image). However, it has no way to catch or clean files left behind by other installers of other software and tools.
IMPORTANT NOTE: The below method will only work if one or both of the below conditions are met. If not, you will not be able to extend the C drive to the newly added space.
- Your C drive lives on a VHDX file on its own (dedicated VHDX file for the system drive)
OR
- Your C drive partition is the last partition on the disk — open disk management console either within the VM being affected or open the VHD file in the Hyper V host’s disk management console and verify if the C drive partition appears on the right hand side most end.
Expand Virtual Hard Disk
So, it is time to expand the system’s virtual hard disk file. However, before you do that, you need to shutdown the VM. Therefore, shut down the VM, and logon to the Hyper V Manager.
Most probably, you are using the Hyper V “Checkpoint”s feature. If so, you will need to merge all of the VM’s checkpoints disks into the main VHDX file. Otherwise, the VHD expansion will not work.
Use the Virtual Hard Disk Editor tool (“Edit Disk” option on the right hand side pane), browse to the VHDX file for the system drive and expand the drive to its desired new size. After the disk file has been expanded, open the VM’s properties and ensure that the drive is set to use the VHDX file and not the previous checkpoint file. Start the VM.
Extend the System Drive
After the VM has booted, you will not find any new drive in the Explorer window. Open Disk Management Console (diskmgmt.msc), where you will see the newly added space as a separate partition — yeap, the expansion actually adds the space as a new partition to avoid data loss within the disk file. Now, hopefully, your C drive is right next to this partition. Now all you have to do is to right click on the C drive volume and select “Extend Volume”. Select the new partition with all available space on it and click OK. And you’re done!
The PowerShell Way
If you want to do all the stuff we did above using the Command Prompt and PowerShell, here are the commands:
A. On the VM
Open an Administrator Command Prompt:
C:\> DISKPART
DISKPART> SELECT DISK 0
DISKPART> LIST PARTITION
Ensure that the C drive partition is listed at the bottom of the list of partitions. If it is not, abort following this walkthrough right here!
B. On the Hyper V Host
Open a PowerShell console. This first set of commands will find your Windows 8 client PC (we are using “Windows 8 Client PC” as the name that has been given to the VM in Hyper V, substitute it for the actual name at your end) and shut it down. After that, we remove all snapshots (checkpoints) for this VM until 1 minute before — which is basically all previous snapshots.
PS\> Get-VM | Where-Object { $_.Name -eq "Windows 8 Client PC" } | Stop-VM
PS\> Get-VM | Where-Object { $_.Name -eq "Windows 8 Client PC" } |
Get-VMSnapshot | Where-Object {$_.CreationTime -lt (Get-Date).AddMinutes(-1)} | Remove-VMSnapshot
The last command above can take quite a while depending on how many snapshots are there. Therefore, open the Hyper V Management console or from PowerShell itself, watch the snapshot removal progress and wait for the VM’s status to become “Operating Normally”. In PS, run the Get-VM
command, optionally with the Where-Object
filter just as first command above (remember to remove the trailing Stop-VM
command).
After the snapshot removal is complete, proceed to expand the drive as below. Replace the path to the VHDX file with the one for your VM’s system disk. Also, I am expanding the drive to a new size of 80GB, modify this as well as per your system’s requirement. After the edit, change the VM’s disk file attachment as well with the next command before proceeding to start the VM.
PS\> Resize-VHD -Path "F:\Disks\VMDISK-CLIENT-WIN8-SYSTEM.vhdx" -SizeBytes 80GB
PS\> Get-VMHardDiskDrive -VMName "Windows 8 Client PC"
PS\> Set-VMHardDiskDrive -VMName "Windows 8 Client PC" -ControllerType "SCSI"
-ControllerNumber 0 -ControllerLocation 0 -Path "F:\Disks\VMDISK-CLIENT-WIN8-SYSTEM.vhdx"
PS\> Start-VM -VMName "Windows 8 Client PC"
Note that we slipped in a Get-VMHardDiskDrive
command above. This will show you a list of the disks attached to the VM. We need the Controller Type, Number and Location information to call the Set-VMHardDiskDrive
command to change the VHDX file path.
C. On the VM (Again, After Extending the Disk File)
Open an administrator Command Prompt:
C:\> DISKPART
DISKPART> RESCAN
DISKPART> SELECT DISK 0
DISKPART> LIST PARTITION
DISKPART> SELECT PARTITION 2
DISKPART> EXTEND
DISKPART> EXIT
Substitute the partition number in the “SELECT PARTITION
” command to the number listed against our old system (C:) drive in the output of the “LIST PARTITION
” command.
You do not need to reboot your system!