Here's how I do it:

1) Make sure the VHD is shutdown (status should be Powered down)

2) In the Virtual Machines folder, right-click the Virtual Machine

3) Click Settings on the menu

4) Select the HD in question and click Modify

5) Select the option to Compact


Using Windows 7 to compact a VHD (using i:\ as an example):

Prior to compacting the VHD run cleanmgr /d c within the VM to delete any temporary files.

  1. Load up computer management -> disk management
  2. Mount the VHD from the actions menu using Attach VHD
  3. Extract precompact.exe from

    C:\Program Files (x86)\Windows Virtual PC\Integration Components\Precompact.iso

  4. Run the following, where the i refers to the drive letter the VHD is attached to:

    precompact.exe -SetDisks:i
    
  5. Defrag the drive. This will require admin privileges so use runas to open command prompt or enter command prompt into the search box and select context item 'run as administrator'

    defrag i: /H /U /X
    
  6. Within disk management select action Detach VHD.

  7. Compact the drive inside Virtual PC settings.

Precompact.exe zeros the VHD – that way using the compactor actually works. Note that in some cases this process can increase the VHD size!