Compact or Resize your VHD file

If you are using dynamic disk for your virtual machine you will soon find out how annoying it is when it grows in size, but when removing the files the disk does not shrink back down. VirtualBox provides tools for compacting a vhdi file but not for vhd. Hyper-v claims to be able to compact, but this often does not work and is also very slow. The solution is to use the built in all powerful Windows Disk Manager.

Run the following commands in an administrative command prompt:

Diskpart

Then type the following commands:

select vdisk File="I:\path\to\your.vhd"
attach vdisk readonly
compact vdisk
detach vdisk

Only takes a couple of minutes and compacts your VHD file to the expected size. No need to mess about with defrag, zeroing out the drive. Just quick and simple.

Daniel Mitchell

Daniel Mitchell