How to use PowerShell or VBScript scripting with Hyper-V

Microsoft's Hyper-V includes scripting options to manage virtualization environments. See examples of how to automate and configure in PowerShell or VBScript.

To effectively manage any virtualization platform, you need a strong arsenal of scripting tools. This can allow...

various levels of automation and shorten configuration time. Microsoft's hypervisor includes scripting options that can use VBScript and the robust PowerShell for Hyper-V script environment. In this tip, I outline how Hyper-V scripting works and show a few examples of how to get started .

Hyper-V's scripting environment is officially called the Windows Management Instrumentation (WMI) , and we will focus on the virtualization namespace. With a WMI interface, administrators have scripts for a wide range of tasks with Hyper-V. And the options for admins get even better with the PowerShell Management Library.

Deciding what script to use depends largely on what is required and what is available. I will focus on Hyper-V servers that are not centrally managed with System Center Virtual Machine Manager (SCVMM), because Microsoft provides additional scripting options for SCVMM implementations. Check out this TechNet page for an overview of SCVMM scripting options.

Microsoft is better at writing scripts
My career has revolved around scripting as needed and working off scripts that I find on the Internet. Chances are Microsoft has a better script than what you or I can create independently. Luckily, there are plenty of resources to get you started. Below I provide a list of some of the best online resources to get started with Hyper-V scripting:

  • Virtual PC Guy's WebLog: Ben Armstrong's Microsoft Developer Network (MSDN) blog is a great source for sample scripts from all Microsoft virtualization systems.
  • Taylor Brown's Blog: This is another strong offering for the Microsoft virtualization camp. Brown covers several use cases, including one for SCVMM.
  • The Microsoft virtualization team blog site: This is a good catchall for everything related to Hyper-V, as well as other virtualization segments.
  •'s Hyper-V PowerPack: This is roughly equivalent to VMware's VI Toolkit and its functionality from PowerGUI is a definite must-have'. Be sure to check out this TechTarget tip by Eric Seibert on this powerful toolkit.

Get VM information example
Now that I've given an overview of the scripting options, let's jump into an example. Nearly every resource for PowerShell scripting with Hyper-V begins with the query script, especially if the script involves deterministic handling. This is a simple one-liner that interacts with the WMI virtualization namespace of Hyper-V and gives information on a VM as it is exists in the namespace. Many scripts will want to query this to get current status on elements such as the OperationalStatus value, which indicates a VM's run state. Here is a sample command to get this information for a VM named "TESTVM1":

Get-WmiObject -Namespace root\virtualization -Query "Select * From Msvm_ComputerSystem Where ElementName='TESTVM1'"

The VM name is the only value to change if you want to run it in your own environment. When executed in PowerShell, the result displays, as shown in Figure 1.

Note that the VM's OperationalStatus value is displayed as "2", meaning it is running.

Stop/start a VM with VBScript example

The basic tasks of starting and stopping a VM are good ways to get started with scripting. Other than PowerShell, Hyper-V machines can interact via VBScript and get full access to the WMI virtualization namespace. This example script performs a startup of a VM that is powered off using VBScript:

Option Explicit

Dim CallWMI

Dim InventoryVMs

Dim YourVM

YourVM = "TESTVM1"

Set CallWMI = GetObject("winmgmts:\\.\root\virtualization")

Set InventoryVMs = CallWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem

WHERE ElementName='" & YourVM & "'")


Like the PowerShell example, the only piece that needs to change in this script is the inline value for "YourVM", which is set with the VBScript variable as "TESTVM1". Saving this text as a .VBS file on the local file system will allow it to execute locally.

Let's now twist this a bit and perform a shutdown on a remote Hyper-V server. The prior example was for running a script locally for a server that has the Hyper-V role and the designated VM running. This can be beneficial in environments with multiple Hyper-V servers and where SCVMM is not implemented. This script will shut down (force power off) the TESTVM1 VM remotely on Server55:

Option Explicit

Dim CallWMI

Dim InventoryVMs

Dim YourVM

YourVM = "TESTVM1"

Set CallWMI = GetObject("winmgmts:\\SERVER55\root\virtualization")

Set InventoryVMs = CallWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem

WHERE ElementName='" & YourVM & "'")


Note that Line 6 enters the name of Server55, which is the Hyper-V server that holds TESTVM1. This can run remotely from systems that are aware of the WMI virtualization namespace, such as another Windows Server 2008 server. The forced shutdown is sent with the Code 3 in the last line of the above example. Other popular VM codes include the following:

  • Reboot (10): This code performs a hard reset on a VM.
  • Pause (32768): This code pauses the VM.

Complete information on the RequestChangeState method of the WMI virtualization namespace can be found online at the MSDN website.

Testing Hyper-V scripts

Hyper-V scripting should be done in a test environment. PowerShell is, as advertised, an extremely powerful shell environment. Scripts written in VBScript present the same riskbecause they do what you tell them to do, as they will also do what you tell them to do.. And when commands are passed, there is no Cancel button or backing out of commands. Simply put, the commands assume that you know what you are doing. Also, consider permissions, which could be a concern with remote Hyper-V servers. Here is a link to a TechTarget tip on the permissions model of Hyper-V.

On with scripting

There are plenty of options for administrators who want to automate elements of their Hyper-V environments. With some practice, care and patience, you can get Hyper-V tuned to your tastes with PowerShell or VBScripts that you create.

Rick Vanover, (MCITP, MCTS, MCSA) is a systems administrator at Safelite AutoGlass in Columbus, Ohio. Vanover has more than 12 years of IT experience and focuses on virtualization, Windows-based server administration and system hardware.

And check out our Server Virtualization blog.

This was last published in June 2009

Dig Deeper on Microsoft Hyper-V and Virtual Server



Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.