With the introduction of Server 2012, Microsoft added 164 PowerShell cmdlets directly pertaining to managing your Hyper-V environment. While you can certainly manage your VMs using the Hyper-V Manager GUI, you can typically find scripts and methods in PowerShell which will save you time and allow you to easily perform tasks you find yourself repeating often. You can see the list of all Hyper-V cmdlets by running:
Get-command –Module Hyper-V
In this post we will step through memory assignment to Hyper-V virtual machines using 5 common cmdlets: Get-VM, Set-VM, Format-List, Stop-VM, Start-VM.
I recommend practicing with the “read-only” Get-VM cmdlet. This cmdlet returns a PowerShell object which can be used in conjunction with the Format-List to dump all or a subset of properties for the VM object. As you can see below, there is a ton of information available for each object:
What I find so helpful about performing these types of exercises in PowerShell is that the cmdlets can perform functions on all of my VMs or on a subset of VMs I specify by using the –Name option. The -Name option accepts wildcards for pattern matching on the name string. This is a huge time saver over clicking through multiple VMs individually in the Hyper-V Manager.
Let’s work through an example where you have added new RAM to your host computer and you would now like to bump up the memory assigned to 4 VMs from 512MB to 2 GB. Using Hyper-V Manager you would have to perform multiple tasks to each of the 4 VMs individually: shut down, open each settings menu, adjust the RAM and restart each one. However with PowerShell, you could perform the entire operation with just a few cmdlets. It’s a good idea to use Get-VM in between tasks to verify your results. Shutdown your VMs, check the current memory assignment, apply the new memory values and display the values after the new assignment:
Then use the Set-VM cmdlet to change the RAM assigned to 2 GB for each and redisplay the new values.
After you review the new memory assignment for your VMs, you can then restart all VMs with a single Start-VM cmdlet:
Once you get comfortable with a few key commands, you will find managing your Hyper-V environment via PowerShell much more effective than using the Hyper-V Manager. Remember, the Get-Help cmdlet is super powerful and will search the help database for partial strings of commands.