PowerShell 2.0 – Script for Shutting Down VM

Here is a simple 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.

# 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")