# Compacting a Dynamic Hyper-V Virtual Hard Drive

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:

1. 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.
2. Shutdown the VM and make a copy of the vhd so you will be working from a backup (obvi…)
3. From the hyper-v host computer type diskpart in a command prompt
4. Type the following commands within the diskpart window:
select vdisk file="C:\path\to\copy\of\backup.vhd"
exit