Managing Hyper-V with PowerShell update from March 2014

If 42 is the answer to life, the universe, and everything, then Windows PowerShell is certainly a close second. The Hyper-V role, specifically, has a vast PowerShell module that enables every aspect of management.

John Savill

March 6, 2014

3 Min Read
Managing Hyper-V with PowerShell

If 42 is the answer to life, the universe, and everything, then Windows PowerShell is certainly a close second. In my experience, PowerShell is the answer for how best to manage Microsoft solutions and automate processes from the command line. The Hyper-V role, specifically, has a vast PowerShell module that enables every aspect of management. You can import the Hyper-V module by using the command Import-Module Hyper-V. (PowerShell v3 and later automatically load of modules, but knowing how manually import them is always a good idea.)

When you consider Windows Server 2012 R2 Hyper-V’s scope of functionality, it’s no surprise that the module contains a huge number of cmdlets. To view these cmdlets, use the Get-Command –Module Hyper-V command. If you’re just getting started with Hyper-V module in PowerShell, I recommend using the PowerShell Integrated Scripting Environment (ISE). The ISE features IntelliSense, to help autocomplete PowerShell commands, and a forms-based interface via the Command add-on, to help you understand which parameters are required and to help you complete actual values, as the figure shows.

Figure 1: Hyper-V and PowerShell

There is really no limit to what you can do with the Hyper-V PowerShell module. Here are some ideas, to get your feet wet:

Create Virtual Hard Disks (VHDXs), both fixed and dynamic

New-VHD -Path D:Virtualsewfix.vhdx -Fixed -SizeBytes 1000000000New-VHD -Path D:Virtualsewdyn.vhdx -Dynamic -SizeBytes 10GB

Create a virtual machine (VM) that uses Dynamic Memory and has two cores

New-VM -Name "TestVM" -Path D:Virtuals -VHDPath D:Virtualsewdyn.vhdx `     -MemoryStartupBytes 512MB -SwitchName "Internal Switch"Set-VM -Name "TestVM" -DynamicMemory -MemoryMaximumBytes 2GB `     -MemoryMinimumBytes 256MB -ProcessorCount 2

Start the VM

Start-VM -Name "TestVM"

Create a checkpoint and view the checkpoint after creation

Checkpoint-VM -Name "TestVM" -SnapshotName "Before change"Get-VM -Name "TestVM" | Get-VMSnapshot

With a little practice, you can get to more interesting actions. For example, the following command exports all VMs that are turned off:

Get-VM | where-object {$_.State -EQ "Off"} | Export-VM -Path D:Backups

Later, you can import the VMs by parsing for the configuration XML files:

Get-ChildItem D:Backups*.xml -recurse | Import-VM

You can even initiate parallel live migrations via a simple workflow:

Workflow Invoke-ParallelLiveMigrate{    $VMLIST =  Get-VM vm1,vm2,vm3,vm4,vm5    ForEach -Parallel ($VM in $VMLIST)    {    Move-VM -Name $VM.Name -DestinationHost targethost    }}Invoke-ParallelLiveMigrate

These are just a basic yet essential PowerShell options for Hyper-V. The key point is that anything you can do with Hyper-V Manager and Failover Cluster Manager—and in reality, much more—you can achieve by using PowerShell. Remember that PowerShell works on remote systems as easily as on a local system and can even perform commands on multiple machines simultaneously. If you use System Center Virtual Machine Manager (VMM), then additional PowerShell modules are installed.

If you want to pick one tool to help you get closer to the answer to life and the universe—or at least the answer to managing your Windows environment—I’d make it PowerShell. For more information about working with Windows Server 2012 R2 Hyper-V, visit Microsoft's Virtualization page.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like