Problem solve Get help with specific problems with your technologies, process and projects.

Top Hyper-V PowerShell cmdlets for basic tasks

Hyper-V PowerShell cmdlets help write scripts, which are more customizable than GUI-based tools. Here are some basic cmdlets for use with Hyper-V PowerShell.

The Microsoft Hyper-V management console and System Center Virtual Machine Manager (SCVMM) are great tools for...

managing virtual machines (VMs), hosts and tasks. As with most graphical user interface (GUI) tools, however, these programs have customization and administration limitations. But PowerShell can fill in these Hyper-V management gaps.

It's not as simple as Hello World, but let's walk before we run.
,

 SCVMM and Hyper-V PowerShell cmdlets open up new capabilities that help streamline virtual environment administration. In this tip, I focus on my favorite Hyper-V PowerShell cmdlets and commands. If you don't have a copy of SCVMM, you can still use PowerShell to perform most tasks, including getting basic information, creating new VMs and performing live migrations.

First, download and install the Hyper-V Powershell module. (You may have to change PowerShell's execution mode so the module works correctly. It can run properly on Windows Server 2008, but in Windows 7, you may need to change the execution policy to unrestricted or bypass. It's not optimal, but be aware that you may have to loosen the execution policy.)

More on Hyper-V PowerShell
How to use PowerShell or VBScript scripting with Hyper-V

Optimizing Hyper-V cluster performance among multiple sites

 After installing the Hyper-V PowerShell module, it's time to try out some basic functions. As you get more comfortable with these PowerShell cmdlets and commands, you can combine them to form more complex and valuable scripts. It's not as simple as Hello World, but let's walk before we run.

Get-VM. This cmdlet is the basis for many scripts, and it's pretty self explanatory. Get-VM retrieves information about VMs and provides a quick view of VMs on a particular host. I use this command often:

Get-VM --Server

Figure 1
(Click image for an enlarged view.)

Note that you must focus on a particular server using the -Server syntax, as you would in the Hyper-V management console.
New-VM, Set-VMMemory, Set-VMCPUCount, Add-NewVirtualHardDisk. This group of Hyper-V PowerShell cmdlets provisions new VMs. The wizard-based GUI of Hyper-V Manager may be easier to use, but if you need to create multiple VMs at once, this method is the way to go.

Figure 2
(Click image for an enlarged view.)

With these commands, a slight modification allows you to add or modify multiple VMs at one time.

New-VM -Server Host1 -Name RobTest2,RobTest3,RobTest4 -Path d:
Set-VMMemory -Server Host1 -VM RobTest2,RobTest3,RobTest4 1024
Set-VMCPUCount -Server Host1 -VM RobTest2,RobTest3,RobTest4 4
Add-NewVMHardDisk -Server Host1 -VM RobTest2,RobTest3,RobTest4 -Size 24GB

Helpful Hyper-V PowerShell definitions
Cmdlets: These are the building blocks for commands. Cmdlets perform an action and provide a Microsoft .NET framework object to the next command. All cmdlets follow a verb, noun format (e.g, Get-VM, Get-Help).

Commands: The execution of one or more cmdlets, along with defining parameters that implement specific functions.

For more information on PowerShell cmdlets and commands, in addition to their differences, visit  Microsoft's  Cmdlet Overview page

 As you can see, simultaneously creating multiple VMs saves time.

Start-VM, Stop-VM, Save-VM, Shutdown-VM. Controlling the state of VMs is also valuable. In some instances where the GUI was not available, I used these commands to control VM shutdowns or save states as a troubleshooting step. Similar to creating and modifying multiple VMs, you can also change the state of multiple VMs at once.
 

Start-VM -Server Host1 -VM RobTest2,RobTest3,RobTest4
Stop-VM -Server Host1 -VM RobTest2,RobTest3,RobTest4
Save-VM -Server Host1 -VM RobTest2,RobTest3,RobTest4
Shutdown-VM -Server Host1 -VM RobTest2,RobTest3,RobTest4

Note: You must have Hyper-V integration components installed to use the Shutdown-VM command.

Move-VM. PowerShell can also help with Hyper-V cluster tasks. I have used the following command as a troubleshooting option to move VMs when Failover Cluster Administrator did not work properly. You can also use the cluster.exe command to move VMs between nodes, but I use this PowerShell command more.

Move-VM -Server Host1 -VM RobTest2 -Destination Host2 --force

It's possible to move multiple VMs, but it must be done sequentially.

Move-VM -Server Host1 -VM RobTest2,RobTest3,RobTest4 -Destination Host2 --force

Or with wildcard, you can move all VMs on Host1 that start with RobTest to Host2.

Move-VM -Server Host1 -VM RobTest% -Destination Host2 --force

(Note: To avoid the "WARNING: Cluster commands not loaded. Import-Module FailoverClusters and tr y again" message and use PowerShell's cluster functionalities, load the Failover Cluster module by entering Run, Import-Module FailoverCluster.)

After you're comfortable with these PowerShell cmdlets and commands, try the commands in the PowerShell Management Library for Hyper-V manual. The manual may put you to sleep, but as you read along and experiment with new Hyper-V PowerShell commands, you'll notice that they share a common syntax. In time, you should be able to orchestrate more complex and productive tasks with PowerShell, which is faster than using Hyper-V wizards to administer VMs.

In part two of this series, I demonstrate SCVMM's extended PowerShell capabilities and share scripts that have helped me in day-to-day, Hyper-V administration tasks.

About the expert
Rob McShinsky is a senior systems engineer at Dartmouth Hitchcock Medical Center in Lebanon, N.H., and has more than 12 years of experience in the industry -- including a focus on server virtualization since 2004. He has been closely involved with Microsoft as an early adopter of Hyper-V and System Center Virtual Machine Manager 2008, as well as a customer reference. In addition, he blogs at VirtuallyAware.com, writing tips and documenting experiences with various virtualization products.


 

This was last published in June 2010

Dig Deeper on Virtual machine provisioning and configuration

PRO+

Content

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.

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close