These PowerShell Scripts help you turn on, turn off, and save guest sessions right from a script. This is extremely helpful when you are trying to turn on a series of guest sessions (possibly on a demo system, or if you have a DR site where you want to fire up a bunch of guest sessions all at once), or if you want to power down a bunch of sessions (or save a bunch of sessions) without having to go to each one of them and manually "shutdown" or save the sessions.
There are 3 scripts here, you would change the information in quotes (ie: "32-Guest Session 3", "32-Guest Session 2", "32-Guest Session 1", etc) to match the name of your HyperV guest session (as it appears IN the HyperV console (not the actual VHD file name), but specifically as you have it named). As with anything in PowerShell, the # is just a comment line, and the $ items are variables so that I can name a variable at the start of the script, and reuse that variable later in the script.
These are 3 different scripts and should be saved individually and then run based on the process (start, save, poweroff) that you want. The line breaks on the blog screws up some of the lines, but effectively EVERY line in the scripts start with a # (comment), $ (variable), or the words read or write, so if you just make sure that the line breaks group together the content logically per line and each line starts with one of the above noted, you should be good on the scripts...
STARTVMs.PS1
# the get-wmiobject identifies the guest session
# the requeststatechange (2) turns on the session (a 3 powers off, 4 shutdown, 32769 saves)
# the |out-null suppresses the "return values" (cco-rhm)
# instead of read-host, could have done a $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") for anykey, but I liked the specific "return key" as the options
# also the readkey function doesn't display in ISE but will actually work
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 1"}
write-host -foreground Green "Starting VM $($VM.ElementName) now"
$vm.requeststatechange(2) |out-null
Read-host "Press Enter to Start Rest of the Images"
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 2"}
write-host -foreground Green "Starting VM $($VM.ElementName) now"
$vm.requeststatechange(2) |out-null
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 3"}
write-host -foreground Green "Starting VM $($VM.ElementName) now"
$vm.requeststatechange(2) |out-null
SAVEVMS.PS1
# the get-wmiobject identifies the guest session
# the requeststatechange (2) turns on the session (a 3 powers off, 4 shutdown, 32769 saves)
# the |out-null suppresses the "return values" (cco-rhm)
# instead of read-host, could have done a $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") for anykey, but I liked the specific "return key" as the options
# also the readkey function doesn't display in ISE but will actually work
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 3"}
write-host -foreground Green "Saving VM $($VM.ElementName) now"
$vm.requeststatechange(32769) |out-null
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 2"}
write-host -foreground Green "Saving VM $($VM.ElementName) now"
$vm.requeststatechange(32769) |out-null
Read-host "Press Enter to Save Main GC Image"
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 1"}
write-host -foreground Green "Saving VM $($VM.ElementName) now"
$vm.requeststatechange(32769) |out-null
PowerOffVMs.PS1
# the get-wmiobject identifies the guest session
# the requeststatechange (2) turns on the session (a 3 powers off, 4 shutdown, 2 saves)
# you may choose to change the $vm.requeststatechange(3) to be $vm.requeststatechange(4) if you want a clean shutdown (recommended)
# as I have it now, the VM just powers down quickly, not cleanly as a "power off" state (cco-rhm)
# the |out-null suppresses the "return values"
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 3"}
write-host -foreground Green "Powering Off VM $($VM.ElementName) now"
$vm.requeststatechange(3) |out-null
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 2"}
write-host -foreground Green "Powering Off $($VM.ElementName) now"
$vm.requeststatechange(3) |out-null
$vm=get-wmiobject -namespace root\virtualization -class msvm_computersystem | where {$_.elementname -eq "32-Guest Session 1"}
write-host -foreground Green "Powering Off $($VM.ElementName) now"
$vm.requeststatechange(3) |out-null
Rand Morimoto has been in the computer industry for more than 30 years and has authored, co-authored, or been a contributing writer for a couple dozen books on Microsoft Windows, Security, Exchange email, BizTalk Server, and remote and mobile computing. Rand is the president of Convergent Computing, an IT consulting firm that has been one of the key early adopter program partners with Microsoft, implementing beta versions of Microsoft technologies 2-3 years before the product releases to the public. This provides Rand and the consultants in his company extensive knowledge on the technologies long before the products are generally available.
Besides speaking at more than 50 conferences and conventions around the world in the past year on tips, tricks, and best practices on planning, migrating, and implementing technologies, Rand is also head judge for the worldwide Imagine Cup competition, is a Board member for the Chabot Space and Science Center and planetarium, and a Regent for the Board of Saint Mary's College of California.
Rand's book Exchange Server 2010 Unleashed was selected as the November, 2009, book of the month book giveaway. Read a free sample chapter of this book,, hosted exclusively by Microsoft Subnet. Buy the book now from InformIT.
Rand's book Windows Server 2008 R2 Unleashed was selected as the Microsoft Subnet January, 2010, book giveaway. Read an excerpt of Windows Server 2008 R2 Unleashed hosted by Microsoft Subnet
Rand's latest book, Microsoft System Center Enterprise Suite Unleashed has been selected as the April, 2010, Microsoft Subnet book giveaway. Read an excerpt of Microsoft System Center Enterprise Suite Unleashed.
Enter the monthly book giveaway contest. Entry form and details are on the Microsoft Subnet home page.