PowerShell 2.0 – ShutDown VM Script

Here is the script for shutting down a Virtual Machine. Have a close look in the script and look for change needed, like your virtual machine name, exit reasons etc. This script is tested under Win2k8 and Win2k8R2 Environment. Thanks goes to the person who wrote this, I can’t remember his/her name or where I collected this script, but had it for a long time now, and used it regularly; now sharing this small script with you all.

# Shutdown a Virtual Machine (requires Integration Components)
 param([string]$vmName = $(throw "Must specify virtual machine name")  )
# Get the VM by name and request state to change to Enabled 
 $vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter 
"ElementName='$vmName'"     # Get the associated Shutdown Component  
$shutdown = gwmi -namespace root\virtualization `      -query 
"Associators of {$vm} where ResultClass=Msvm_ShutdownComponent"
# Initiate a forced shutdown with simple reason string, 
return resulting error code  
return $shutdown.InitiateShutdown($true,"System Maintenance")