Microsoft Enterprise Platforms Support: Windows Server Core Team
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:
Robert Simpkins Senior Support Escalation Engineer Microsoft Enterprise Platforms Support
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