Monthly Archives: June 2016

Shutting Down Running VMs – Revisited

A couple of years ago, I posted a perfectly good snippet for shutting down the running VMs on a machine. But the code there is very much the "old syntax" and not terribly elegant.  For shutting down all the running RODCs, I used:

Get-VM -Name *rodc* | Where-Object {$_.State -eq "Running" } | Foreach-Object { Stop-VM $_.Name }

Which is a nuisance to type, frankly. So, here's the PowerShell v5 version. Much slicker, much easier to remember, and a lot quicker to type.

Get-VM -Name *rodc* | Where State -eq "Running" | Stop-VM

Simpler and easier to follow. It still requires a two-pipe solution, but that's only because we only wanted to stop the running VMs without any warning messages. If we didn't care about spurious warning messages, the answer gets even simpler:

Stop-VM *rodc*

All things considered, I'm in favour of simpler. And the odd warning message doesn't concern me, so I'll go with the last solution 90% of the time. Note that this accepts a -ComputerName parameter for running against a remote computer, a -PassThru parameter for echoing out which VMs it's shutting down, and a -TurnOff parameter to just kill the VMs, rather than use an orderly shutdown.

 

Historical note:  If you want to see a really complicated way to do this, written before we had "Stop-VM" as a cmdlet, take a look at this post from 4 years ago! I'm so glad PowerShell is doing all the heavy lifting now!