Find orphaned VHDs in Hyper-V cluster
If you’re running a Hyper-V cluster for a while, you probably ended up with a couple of virtual hard disks for which the actual VM is already gone. These orphaned VHDs are occupying valuable space and are kinda difficult to trace. I decided to dive into this and hoped that someone has already solved this puzzle.
I found the following scripts which advertise to do the job:
- Free script find orphaned Hyper-V VM files
This one is 346 lines of code and always ends up with an error for me. - Get-OrphanedVHDs.ps1 from alexinslc
Simple set-up but uses csv files and looks to be made for local storage Hyper-V nodes.
That didn’t really help so I decided to be bold and write my own version. Mine is meant to be run on clusters where all of your Hyper-V nodes use the same storage like with a SAN-based setup. It’s 36 lines and gets the job done within a minute for over 200 VMs.
You can run this Powershell script against any one of your nodes directly or by running it through Invoke-Command.