I have used Hyper-V for as long as there has been Hyper-V. Today I use it much less than I once did, but it is still handy for running VMs on my laptop. I run a particular VM called ‘Sandbox’ in which I do all sorts of things that I would not want to do on my live system… things that I can simply try and then wipe.
When I built the Sandbox VM I was spending a lot of time at home, and portability was not a huge issue. I ran it on one of my external drives, and it worked fine. I allocated 100GB and was good to go.
When I realized I was going to be on the road again I could just as easily take my external hard drive with me, but the shortage of USB ports on my Surface Pro meant making a decision… I was going to shrink the VHDX file and put it on my internal hard drive.
76GB free space. That’s going to be a problem.
Step 1: Shrink your partitions
My 100GB virtual hard drive (.vhdx file) meant that somewhere within the VM I had a 100GB partition (or at least a few partitions that added up to that). I had to shrink as much as I could.
- If you have a Pagefile.sys, Swapfile.sys, and Hiberfil.sys you should eliminate them now. Remember that even if you turn off Memory Paging the files don’t disappear until you reboot.
- Defragment the disk. We may not talk much about it anymore, but the old faithful defrag.exe C: still works.
- Use the Disk Manager console to shrink your C: as much as you can… but not too much. When I tried it I had the option to shrink it down to 11.5 GB… I’m pretty sure that would render my VM pretty useless. Pick a number that works for you. I chose 60GB.
- Using the diskpart tool delete any partitions at the end of your drive. I had a 450MB Recovery partition on mine.
Because it was a Recovery (read: SYSTEM) partition I needed to do the following:
Select Partition 5
Delete Partition OVERRIDE
Good… Now we can shrink the VHDX file.
In the older versions of Hyper-V this would have meant shutting down the VM. You don’t have to do that anymore… but you do have to run PowerShell as an Admin. Once you do:
Step 2: Shrink your VHDX file
The cmdlet is easy…
- Navigate to the directory where you keep your VHD file;
- Resize-VHD -Path .\Sandbox-PC.vhdx -SizeBytes 60GB
It will only take a minute and you will be done. Simple as pie!