Posey's Tips & Tricks
How Much Space Do You Get by Shrinking a Hyper-V VHD?
The answer lies in Hyper-V's Edit Virtual Hard Disk Wizard. Here's a walk-through.
One of the Hyper-V features that I have found to be the most helpful over the years is the Edit Virtual Hard Disk Wizard.
This wizard, which you can see in Figure 1, has the ability to expand a virtual hard disk (VHD) to allow it to accommodate additional data. It can also convert a VHD from one architecture to another. For instance, you could use the wizard to convert a VHD file into a VHDX file.
As you look at the figure above, you will notice that the default option is Compact. This option is especially helpful for thinly provisioned (dynamic) VHDs because it allows you to reclaim wasted disk space.
In case you are not familiar with the Compact option, the basic idea is really simple. A dynamically expanding VHD is designed to initially consume only about 4MB of space regardless of its provisioned size. As data is added to the VHD, the VHD file grows to accommodate the data until it eventually reaches the maximum size that was specified at the time the VHD was created.
However, while the VHD file grows as data is added, it does not shrink when data is deleted. Using the Edit Virtual Hard Disk Wizard's Compact option shrinks the VHD file, thus allowing you to reclaim the space occupied by data that has since been deleted.
Although compacting a VHD is a really straightforward process, you may sometimes find that compacting the disk does not yield nearly as much physical disk space as you think it should. Let's take a look.
In preparation for this article, I created a new VHDX file. This file is 127GB in size and initially consumes 4MB of disk space, as shown in Figure 2.
The next thing that I did was to create a small (48.8GB) volume on the VHD. Now, take a look at the effect that this has on the VHDX file. As you can see in Figure 3, the file has grown from 4MB to about 330MB without me adding any data.
So the next thing that I did was to copy roughly about 10GB of data to the volume on the VHD. It takes a few minutes, but the VHD file eventually grows to accommodate the data, as shown in Figure 4. As previously noted, deleting the files has no effect on the size of the VHD.
Now the question becomes a matter of how small the VHD will be after I compact it. Let's turn to PowerShell for some answers. You can use the Get-VHD cmdlet to display information such as the file size and the VHD size, but the output uses large numbers that can be a bit hard to follow. I am going to do things a little bit differently.
I began by creating a variable called $A, and set that variable equal to Get-VHD *. This allowed me to output size information in GB. For example, to see the VHD size, I simply typed $A.Size/1GB. You can see the output of my commands in Figure 5.
So what this information is telling me is that the VHD size is 127GB. The current file size (the amount of physical disk space being consumed by the VHDX file) is 10.9GB and the minimum size is 48.95GB. In other words, if I were to attempt to compact the virtual disk, I would not gain any space, because I cannot shrink it any smaller than 48.95GB (even though the file is currently nowhere near that big). The reason for this is that I currently have a 48.8GB volume on the disk. If I had deleted a substantial amount of data and wanted to compact the disk, I would need to first shrink the volume.
For the sake of example, I have deleted the volume and executed the cmdlets again. This time, you can see that the minimum size is only about 125MB. Hence, we can see that volume size plays a major role in compacting a VHD.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.