Using PowerShell to Turn HyperV Guest Sessions On/Off/Save

Analysis
Apr 3, 20114 mins

Automating HyperV Guest Session Tasks

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 rootvirtualization -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 rootvirtualization -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 rootvirtualization -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 rootvirtualization -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 rootvirtualization -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 rootvirtualization -class msvm_computersystem | where {$_.elementname -eq “32-Guest Session 1”}

write-host -foreground Green “Saving VM $($VM.ElementName) now”

$vm.requeststatechange(32769) |out-null

# the get-wmiobject identifies the guest sessionPowerOffVMs.PS1

# 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 rootvirtualization -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 rootvirtualization -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 rootvirtualization -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

Rand is a Microsoft MVP and security specialist with expertise in Office 365, Microsoft Azure, Exchange, SharePoint, SQL, Windows Server, Windows Client, System Center, and Lync. Rand has over 50 international bestselling books and speaks at conferences and conventions somewhere in the world every month. Rand is also the owner of the consulting firm Convergent Computing, which was Microsoft's Global Partner of the Year (2014) and an early adopter organization across all of the Microsoft products and services.

More from this author