Drive Space Nightmares with Hyper-V

As you know, I have been using Hyper-V since before it was released, and am a huge proponent of the solution (although I am also a huge proponent of VMware).  The fact that Hyper-V is also included in Windows 10 makes my life easier – I use it on my Windows Client for several reasons.  In fact, at present I have four virtual machines on my Surface Pro 4, two of which I use on a very regular basis.

So when I notice from time to time that my C: drive is running out of space, I know immediately what the culprit is… my dynamically expanding drives have, in a word, expanded.

image

Not good… I need more than 3.18GB free space to be comfortable.  However when I look at the drives, I know that none of them are overly taxed… the VM I use most often (I use it to download files that I am not sure are safe so that I can ‘Sandbox’ them) is a dynamically expanding virtual disk that is as much as 80GB, but only 31GB is used.

image

That should be very comfortable… and yet there we see the usage.

image

A 53GB vhdx file for about 31GB of information.  It is easily explained of course… With a dynamically-expanding virtual hard disk the file gets bigger when you write to it, but when you then delete files and clean it up the file does not get smaller… or at least not automatically.  So what you have to do is this:

  • Shut down the virtual machine.  You cannot edit the disk while the VM is running.
  • In the Action Pane of Hyper-V Manager Select Edit Disk…
  • Click Next on the Before you Begin page.
  • In the Locate Virtual Hard Disk page navigate to the ‘offending’ vhdx file then click Next.
  • On the Choose Action page click the Compact radio and click Next.
  • On the Complete the Edit the Virtual Disk Wizard click Finish.
  • At this point the process will begin, and when it is done you should be good to go.

    PowerShell

    Yes, I know… you can do everything you want in the wizard… but let’s try a quick PowerShell cmdlet anyways Smile

    Optimize-VHD -Path C:\Hyper-V\Sandbox-PC\Sandbox-PC.vhdx -ComputerName MDG-SP4

    It only took a couple of minutes, and here are the results:

    image

    Almost 10GB freed up.  That makes life so much more comfortable.  Of course, since I use that virtual PC for these purposes a lot, I will want to keep an eye out for this creep and perform this script on a regular basis.  Hence why you might want to use PowerShell over the GUI.

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s