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.
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.
Senior Support Escalation Engineer
Microsoft Enterprise Platforms Support