If you have ever tried to use the Hyper-V “compact” command on a dynamic vhd, you have probably noticed that it very rarely reduces the vhd file size. If you ever delete files from within the guest OS, reducing the size of the volume, you will notice that the VHD doesn’t get smaller. I found a very quick and easy way to reduce the size of dynamic vhd’s:
- For best results, first defrag the drive from within the guest (optional). If you know you have empty space on the volume, this step isn’t really necessary.
- Shutdown the VM and make a copy of the vhd so you will be working from a backup (obvi…)
- From the hyper-v host computer type diskpart in a command prompt
- Type the following commands within the diskpart window:
select vdisk file="C:\path\to\copy\of\backup.vhd" attach vdisk readonly compact vdisk detach vdisk exit