Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In windows 7 we now have the ability to create and attach VHD files as drives. Does anyone know how we can compact the dynamic disks from within windows 7?

share|improve this question
up vote 13 down vote accepted

You can compact a VHD in Windows 7 using diskpart.exe's COMPACT option. Also, if you don't mind a little coding, you can call the CompactVirtualDisk API (I'd post a link to it, but new users can't post hyperlinks - just search MSDN for it if you're interested).

share|improve this answer
aweseme - thank you! To add to this, you have to attach it read only. commands are: select vdisk file="C:\MyVhd.vhd", attach vdisk readonly, compact vdisk – Scott Ivey May 31 '09 at 16:15
@scott - Followed by detach vdisk – Greg Jan 25 '10 at 19:02

Using the diskpart command line utility in Windows 7 to compact a VHD file. It works great!

alt text

share|improve this answer

when in diskpart console type "select vdisk file="c:\windows7.vhd" then press enter then type "compact", that should compact your virtual drive.

share|improve this answer
that's "compact vdisk" just compact will get the help line for the command. – Ralph Shillington Feb 27 '10 at 15:43

To Compact a VHD file in Windows Virtual PC (in Windows 7): -Open "Windows Virtual Machines" shell -Right-click on the VM you want to compact and open its Settings - Go to "Hard Disk 1" or anyone you have the VHD file attached to. - Click on Modify, then Compact Virtual Hard Disk

share|improve this answer

Did you try the disk manager?

share|improve this answer
I poked around in disk manager, didn't find anything in there. – Scott Ivey May 29 '09 at 18:09

How about VHD Resizer?

share|improve this answer
From what i've seen - VHD Resizer will expand files or shrink files, but will not compact files while leaving their original max size intact. – Scott Ivey May 29 '09 at 18:12
Well that's disappointing, I had that filed away to take a look at later on for just this purpose. Thanks for the heads up. – Kara Marfia May 29 '09 at 18:42

What I ended up doing was to detach the VHD from within Disk Manager, and then i attached the file to my "Virtual Windows XP" in Windows 7. Once that was done, i was able to choose "Modify" on the disk, and then "Compact."

Once the compact was done, i removed the VHD's from the "Virtual Windows XP" VM - and then can use them as needed from there.

This seems like a kludgy solution - i was hoping someone would have a way to run the compact without having to attach them to the VM first, or better yet a way to run it thru power shell or script.

share|improve this answer

  1. Open your Windows command line (CMD).
  2. diskpart
  3. select vdisk file="X:\xyz.vhd"
  4. attach vdisk readonly
  5. compact vdisk
  6. detach vdisk
  7. exit

I am using a virtual hard disk including bitlocker. To compress a VDH file with bitlocker, following steps worked for me:

  1. Open Windows disk management.
  2. Append new virtual disk and choose read only mode.
  3. Unlock your new Windows drive with bitlocker.
  4. Open your Windows command line (CMD)
  5. diskpart
  6. select vdisk file="X:\xyz.vhd"
  7. compact vdisk
  8. detach vdisk
  9. exit
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.