Shrinking disks on your Hyper-V guests

A few of my guests run on SSD drives. Occasionally I reclaim diskspace by defragmenting and compacting the VHD;s. I have managed to free up to 20GB diskspace on a single guest, which is pretty much SSD wise. However, first of all, you should consider before putting R/W intense operations, such as VMs, on your SSD;s.


Since this is a process that will take quite some time I wanted to make it as automated as possible.


1. Defrag with Raxco perfect disk. This script is run remotely on the target machines. Errorhandling is omitted for readability.


2. Compact VHD:s. Run in powershell on the Host. Make sure the actual VM is turned off.