Cleaning up after VM upgrade with PowerShell in Windows “8”

Sometimes little things bug me.  Often – while these little things are annoying, it is too much effort to actually fix them.  Thankfully – Hyper-V PowerShell gives me an easy way to “fix small annoyances” quickly.  One example for a “little thing that bugs me” occurs when you upgrade the integration services for a virtual machine: we do not eject the integration services ISO file when we are done.

The result is that I end up with a bunch of virtual machines with this DVD in their DVD drives.  There is no real downside to this – but it is annoying.

The other day I sat down and figured out a PowerShell one-liner that would check all my virtual machines and would eject the DVD – only if it was the integration services installation DVD image.  Here it is:

get-vm | get-vmdvddrive | %{if($_.path -eq “$env:windirsystem32vmguest.iso”) {Set-VMDVDDrive -VMDVDDrive $_ -Path $null}}

It gets each virtual machine, gets the DVD drives assocciated with each virtual machine and then removes the DVD image if it is the integration services DVD image.

Nice and simple – and now I am much happier with the state of my system.