How to Compact a Virtual Disk (.vhdx)

I was frustrated with the results when I tried to compact virtual hard disks from within the Hyper-V Manager using the Edit Disk options because I found that it didn’t really do anything to reduce the size of my virtual disks so I resolved to find a better way to do so without using third party tools.  It took me a bit of research to find how to do this but after reviewing lots of different options I settled on the method below.  I like the fact that you don’t need to shrink partitions from within the guest operating system and you don’t even need to zero out unused disk space as a lot of the other guides recommended.

Make sure that your vhd or vhdx is not mounted or in use by any operating system or virtual machine, and then, from an elevated command prompt enter the following commands:

diskpart
select vdisk file=”C:\Hyper-V\sampledisk.vhdx”
attach vdisk readonly
compact vdisk
compact vdisk [running the command twice works better than just once]
detach vdisk
exit

If you didn’t get any warnings, your output should look something like this:

Compact Vdisk commands

This process reduced the size of my Master Image from 24GB to 15GB and takes seconds.  It may sound too good to be true but I would be very happy to hear back from you if it worked and you had good results with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.