A client accidentally copied a 200GB file to a Hyper-V guest OS server and inflated the dynamically expanding VHDX of the server drive. I’ve heard rumor that Server 2012 Hyper-V is supposed to automatically resize these back. It didn’t. But whatever, here is what I had to do to shrink it back after removing that big file

Oh, and the Hyper-V GUI “condense” didn’t do anything. No progress bar, just flashed and went away – but it’s an easy powershell comandlet.

  1. Shutdown the guest OS
  2. Open powershell
  3. In the powershell run this to mount the VHDX in read-only mode. Mount-Vhd -path <THE FULL PATH TO THE VHDX FILE> -readonly
  4. Then run this to start the optimize
    Optimize-Vhd -path <THE FULL PATH TO THE VHDX FILE> -Mode quick
  5. Unmount it by running
    Dismount-Vhd -path<THE FULL PATH TO THE VHDX FILE>

Remember to put quotes or apostrophes around your filepath if it contains spaces.

The official commandlet documentation on Technet:
http://technet.microsoft.com/en-us/library/hh848458.aspx