Hyper-V, Hyper-V v2, Scripting, Server 2008 R2, Virtualization, Windows
Leave a comment

Hyper-V: finding stale VHDs

I use Hyper-V Server 2008 R2 for running many virtual machines for different purposes. For all of these virtual machines, I either create new VHDs and re-use the existing ones. Here is the problem. I don’t have SCVMM or any similar management software to maintain the VHD library. But I need to keep track of various VHDs because any accidental deletion might lead to unusable virtual machine infrastructure. VHD Shell Extensions script I created a few months back came to my rescue here. If you have not downloaded or started using this script yet, you can do some by visting this page on MSDN Code gallery.

So, I just created a batch file wrapper around the VHDShellExt script. Here is what I have

@echo off

:: Find all VHDs from the given VHD Path and use VHDShellExt.vbs
for /f %%a IN (‘dir /b /s %VHDPATH%\*.vhd’) do “cscript %SCRIPTPATH% /action:detail /vhd:%%a”

 %SCRIPTPATH% is the path to VHDShellExt.vbs and %VHDPATH% is the place where all your VHDs are stored. This script will validate all the VHDs against the local Hyper-V instance and attempts to find if any of the virtual machines are associated with the given VHDs. Since we are using VHDShellExt.vbs for finding stale VHDs, the output of the script will be similar to what you would usually see when you use VHD Shell extensions. So, for each VHD, there will be one line in the output that specifies if the VHD is associated with a VM or not. I will modify VHD Shell Extensions a bit to provide a different Hyper-V instance than just the loca one. That way, we will be able to use the same script even to check other Hyper-V instances on the network to find stale VHDs.


Filed under: Hyper-V, Hyper-V v2, Scripting, Server 2008 R2, Virtualization, Windows


Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.