Compacting a Dynamically Expanding virtual hard disk in Windows Server 2012

Compacting a Dynamically Expanding virtual hard disk in Windows Server 2012

  • Comments 13
  • 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.

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

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • technet.microsoft.com/.../hh848458.aspx

  • 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 "

    Cheers

  • 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

  • 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.