This weeks PowerShell Friday is about stopping VMs with PowerCLI.

If you want to gracefully shutdown your VM, which I think you will do most of the time you are better of using Shutdown-VMGuest . This will use the VMware tools to gracefully shutdown your VM. Just as with starting VMs you have the option of specifying the VM on the command line or get it from the pipeline.

This command will shutdown all VMs starting with an ‘A’

Checking if the VM is running

If you try to stop VMs that aren’t running you get errors and your commands don’t continue. To check if a VM is running and then stopping it can be done with testing the state:

Don’t ask for permission, ask for forgiveness

A lot of, if not all, cmdlets in PowerCLI obey the -Confirm. With it you can tell the cmdlet you are executing if you want it to ask for permission, or just execute the command. The complete line above would look like this:

If you’re not sure, try the -WhatIf  parameter, or think some more before you execute the command. PowerCLI then will simulate what would happen if the cmdlet was executed, without actually doing anything.

Stopping VMs without VMware tools

If your not running VMware tools in the VM you can stop the VM with the cmdlet Stop-VM

It will stop your VM without saving anything, so be absolutely sure if you need this. Again, if you use the -Confirm parameter the VM will just be stopped without asking for permission.

If that doesn’t stop the VM you can always use the kill switch

 

If that didn’t do the trick…