Compact a VHD file created by Disk Management?

Solution 1:

Compacting can't be completed on a VHD that is attached unless it is read-only.

To detach the disk, you need to right-click the VHD disk in Disk Management and select Detach VHD:

Detach VHD

You can then complete the compacting using diskpart from a Command Prompt:

  1. Run diskpart
  2. Select the disk via its path: select vdisk file="<path>"
  3. Attach it as read-only: attach vdisk readonly
  4. Compact it: compact vdisk
  5. Upon completion of the compact, detach it again: detach vdisk

You can then re-attach it via Disk Management in its normal and newly compacted state.

(Reference: http://linhost.info/2011/01/windows-how-to-compact-a-dynamic-vhd/)