I was recently tasked with cleaning up a client’s Hyper-V environment. This particular environment had experienced some pretty bad VM sprawl. Even worse when VMs were migrated to cluster, or removed, the files were not always cleaned up properly. The environment has over 200 VMs, so it would have taken forever to go through and check each VM and file individually. So to make my life much easier, I created a PowerShell script to help me out.
This script will parse all of the Cluster Shared Volumes in a cluster, and create a PowerShell object with all the files in them. It will then create another PowerShell object for all the Virtual Hard Disks, ISOs, and Snapshot files associated to the Virtual Machines. Then it will compare the two objects, and output the results to a CSV file.
The script must be ran from a server that is a member of the cluster, so that it can access the Cluster Shared Volumes. To run the script simply supply the cluster name, and optionally a location for the CSV file to be exported to.
Depending on the size of your environment this script can take some time to run, so I created a progress bar to let you know that it is still running.
Once the script completes it will output the CSV file path.
The CSV will contain a status for each file. The status of “Attached” is for ones that are associated with a virtual machine, and “Not Attached” for those not associated to a virtual machine. If it is attached it will also list the specific machine name. It will exclude any of the Hyper-V Virtual Machine configuration files. These are files with the extension types of xml, bin, vsv, and exp.
Here you can see I have several files sitting on the Cluster Shared Volumes that don’t need to be there. I can now go move these to a more appropriate storage location or delete them as needed.
You can download the script from the TechNet Gallery – Cluster Shared Volumes Cleanup Script
This script is provided “AS-IS” with no warranties of any kind. Although tested and found as error-free, no responsibility is taken for any misuse of the script, or damage caused by using it. It is recommended that you making full backups of your environment prior to making any major changes.