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 considerR2 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.
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:\Virtuals\newdyn.vhdx -Dynamic -SizeBytes 10GB
Create a virtual machine (VM) that uses Dynamic Memory and has two cores
-MemoryStartupBytes 512MB -SwitchName "Internal Switch"
Set-VM -Name "TestVM" -DynamicMemory -MemoryMaximumBytes 2GB `
-MemoryMinimumBytes 256MB -ProcessorCount 2
Start the VM
Create a checkpoint and view the checkpoint after creation
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:
Later, you can import the VMs by parsing for the configuration XML files:
You can even initiate parallel live migrations via a simple workflow:
$VMLIST = Get-VM vm1,vm2,vm3,vm4,vm5
ForEach -Parallel ($VM in $VMLIST)
Move-VM -Name $VM.Name -DestinationHost targethost
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.