Freigeben über


Compacting a Dynamically Expanding virtual hard disk in Windows Server 2012

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.

clip_image001

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

clip_image002

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.

clip_image003

Follow the Wizard and choose the Compact option

clip_image004

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

clip_image005

clip_image006

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

Comments

  • Anonymous
    January 01, 2003
    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...
  • Anonymous
    September 20, 2012
    technet.microsoft.com/.../hh848458.aspx
  • Anonymous
    November 06, 2012
    Thank you for this. I struggled to figure this out. Couldn't you at least have a message somewhere instead of just not-compacting?
  • Anonymous
    December 16, 2012
    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.
  • Anonymous
    December 29, 2012
    The comment has been removed
  • Anonymous
    January 23, 2013
    The comment has been removed
  • Anonymous
    February 22, 2013
    Hey its A Nice Post….. Wana Know About That Just Log On To www.smeworld.org/.../mcubx-cloud-receptionist-it-solutions-for-small-businesses-1386.php
  • Anonymous
    August 08, 2013
    The comment has been removed
  • Anonymous
    October 15, 2013
    Do we need to apply the same exact procedure for windows server 2012 R2 as well?
  • Anonymous
    November 05, 2013
    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.
  • Anonymous
    December 16, 2013
    Pingback from Online Shrink of VHDX | Resellernews.com.au
  • Anonymous
    December 16, 2013
    Pingback from Online Shrink of VHDX | Resellernews.com.au
  • Anonymous
    January 27, 2014
    You don't need to mount VHD to optimize it.
  • Anonymous
    August 25, 2014
    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.
    spkani@yahoo.com
  • Anonymous
    January 30, 2015
    Great. Works a treat. Thanks :-)
  • Anonymous
    February 05, 2015
    Please clarify - is it recommended to defrag the vhd itself, or the drive containing it, or both?
  • Anonymous
    July 22, 2015
    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.
  • Anonymous
    September 15, 2015
    I know that it should work in Win Server 2012 and it did in Win 8.1 as well. But it does not work in Windows 10. After you attach VHDX the only option in Edit Virtual Hard Disk is Expand. What has been changed?
  • Anonymous
    September 28, 2015
    I've server 2012-r2 running as virtual machine inside VMware sphere, so I will not be able to use Hyper-V. Any Idea how to reduce the white space in the VHDX files in this case?
  • Anonymous
    March 23, 2016
    HiI've seen nothig of this works, unless your REMOVE the VHDX from the virtual machine.More a bad thing: if you shut down the virtual machine and apply these procedures, no errors are visualized, and the VHDX remains the same size :-(Giulianoz