Compacting a Dynamically Expanding virtual hard disk in Windows Server 2012

Compacting a Dynamically Expanding virtual hard disk in Windows Server 2012

  • Comments 19
  • Likes

The process to compact a dynamically expanding virtual hard disk (vhd) in Windows Server 2012 has changed slightly from previous versions of Windows.

It is still recommended that you defrag the drive in advance. Also be aware, if the virtual hard disk is not NTFS formatted, you must prepare the virtual hard disk for compacting by using a non-Microsoft disk utility program to replace the blank space with zeroes. Lastly, the vhd cannot be in use.

In order to shrink the vhd you must first have an account with administrative privileges on the system mount the vhd as read-only prior to attempting to shrink the disk.

One way to mount the vhd is via Disk Management.


Make sure to place a check mark in the box for Read-only.


Another way to mount the vhd is using PowerShell with the following command.

Note: You must have the Hyper-V Module for Windows PowerShell installed. This is listed under features, under Role Administration Tools \ Hyper-V Management Tools

Mount-Vhd –path <full path the vhd file> -readonly

The next step is to compact the vhd.

Using Hyper-V manager, use the actions menu and select Edit Disk.


Follow the Wizard and choose the Compact option


You can do the same as above using PowerShell with the following command:

Optimize-Vhd -path <full path the vhd file> -Mode Full

Once the process is done you just need to dismount the vhd.

With Disk Management, right click on the left end of the VHD and select DetachVHD



From PowerShell the command is

Dismount-vhd -path <full path the vhd file>

The Optimize-VHD command has several options that can be used depending on if the virtual disk is using the VHD format or the VHDX format. Please see the Optimize-VHD command on technet for more information.

To simplify the above process I created the following PowerShell script.

I put the following into a file named compact-vhd.ps1

Param([string]$Path = $(Throw '-Path is required'))
Echo "Attempting to Mount $Path"
Mount-vhd -path $Path -readonly
Echo "Attempting to compact $Path"
Optimize-vhd -path $Path -mode full
Echo "Attempting to dismount $Path"
Dismount-vhd -path $Path

The syntax is .\compact-vhd.ps1 -Path <path to vhd>

A few notes for the script:

  • For this script to work, you must run PowerShell as an Administrator.
  • Since this is an unsigned script, you will have to adjust the ExecutionPolicy in PowerShell or sign the script.
  • This script is a sample. Use at your own Risk. There is no support for the above script.

Robert Simpkins
Senior Support Escalation Engineer
Microsoft Enterprise Platforms Support


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment

  • Thank you for this. I struggled to figure this out. Couldn't you at least have a message somewhere instead of just not-compacting?

  • Thank you for these instructions. Why is this not documented anywhere? An existing feature no longer works as expected - please put a note in the UI, or the help file, or the technet library documentation, etc...

  • My mistake on previous post: went too fast :) - it looks like you need to have the VHD mounted read-only for certain optimization modes. However, I was able to use all optimization modes w/o mounting before and didn't get an error or something.

  • Oh my god. Why the hell is this in the OS? If you have to Google why your server isn't doing something it used to do, your software is broken. Throw a frickin error!!! GOL!!!!!!!!!!

  • Hello! please advise the PS script has an error:

    This line "Optimize-vhd -path $Path -full "

    Should be: "Optimize-vhd -path $Path -mode full "


  • Hey its A Nice Post….. Wana Know About That Just Log On To

  • As others have said, there needs to be more help in the OS other than the dialog box disappearing with no action.  This is a regression.

  • Do we need to apply the same exact procedure for windows server 2012 R2 as well?

  • I can confirm that one 3 separate server 2012 R2 hyper v hosts that compaction still does not work through the hyper-v management console, PowerShell commands seem to be the only way to do this.

  • You don't need to mount VHD to optimize it.

  • How frequently I need to do the compacting of the Guest VM's. If you good tell me for windows 2008 R2 also it is great help.

  • Great. Works a treat. Thanks :-)

  • Please clarify - is it recommended to defrag the vhd itself, or the drive containing it, or both?

  • Disk Optimization on my Windows Server 2012 R2 Hyper-V Host did not work on my .VHD. Once I converted the VHD to a VHDX it worked successfully, and then I could compact it. Not sure if it matters, the OS contained in the VHD was Server 2008 R2. The VHD was original created on a Server 2008 R2 Hyper-V Server, years ago.