How to shrink Windows 7 XP Mode VHD files?

28,733

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!

Share:
28,733
A_M
Author by

A_M

Updated on September 17, 2022

Comments

  • A_M
    A_M over 1 year

    I'm trying to shrink a Windows 7 XP Mode VHD file with VhdResizer with little success.

    When I select my VHD file, it says "VhdExpand only supports fixed and dynamic VHD files". My XP Mode VHDs are dynamic files.

    Does anyone have any idea why it is failing?

    Failing that, does anyone have a process that I can use to shrink my XP mode VHD files on Windows 7 (64 bit)?

  • Admin
    Admin about 14 years
    You may want to zero the non-used sectors of the VHD in the guest OS first, otherwise compacting might not do much.
  • David Balažic
    David Balažic about 9 years
    Shouldn't defragging be done before precompacting?
  • Kvad
    Kvad about 9 years
    Looking back now. Probably! YMMV.
  • user1251007
    user1251007 about 6 years
    Could someone explain the downvote?