Hyper-V How To: Shut Down VMs using Script

Some friends here on the Hyper-V team shared a PowerShell 2.0 script for shutting down your VMs:

# Shutdown a Virtual Machine (requires Integration Components)

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

For more info on how to use PS cmdlets see: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx

See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!

For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download

Comments (0)

Skip to main content