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.
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.)
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:
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.
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
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.