While the Hyper-V management console is cozy and can certainly get the job done, I think there are a handful of PowerShell cmdlets that can make your life easier. At least in terms of managing Hyper-V. These are commands that have a graphical counterpart, although sometimes it takes a little work to get to it. These are my top 10 cmdlets every Hyper-V pro should know, but in no particular order. All of these commands have help files so be sure to check them out as well. All references are to Hyper-V cmdlets that run on PowerShell 3.0.
1. Get-VM
I’d have to say the one command used the most is Get-VM. It does exactly what its name implies as you can see in Figure 1.
Figure 1
Remember that what you are seeing are virtual machine objects and there is more than this default display.
Figure 2
As you can see, you can get virtual machines by name. Or you can filter on any of the virtual machine parameters. For example, I usually only want to see running virtual machines so I can use a command like this.
PS C:> get-vm | where {$_.state -eq 'running'} | sort Uptime | select Name,Uptime,@{N="MemoryMB";E={$_.MemoryAssigned/1MB}},Status
I even went ahead and sorted on the Uptime property and formatted the assigned memory as MB. You can see the results in Figure 3.
Figure 3
Want to get to know Get-VM better? Have a look at our Deep Dive into Get-VM
2. Start-VM
Of course seeing a virtual machine is one thing. Controlling them is equally important. Starting a virtual machine couldn’t be any easier.
PS C:> start-vm chi-win8
If it will take some time to start the VM or if you are starting many of them, you can use the –AsJob parameter to push the task to the background.
PS C:> start-vm jdh* -asjob
3. Stop-VM
On the flipside, stopping a virtual machine is just as easy. PowerShell once again makes it much easier when dealing with multiple virtual machines.
PS C:> stop-vm jdh*
The shutdown is made through the guest operating system. If the VM has open applications or unsaved data, it will wait 5 minutes before shutting down, unless the operating system shows the screen is locked. You can always force the shutdown with the –Force parameter. In worse case situations you can tell the cmdlet to simply kill power to the virtual machine.
PS C:> stop-vm "XP Lab" –turnoff
The other option you have is to stop the virtual machine but put it in a saved state.
PS C:> stop-vm chi-win8 –save –force
4. Checkpoint-VM
Use the Checkpoint-VM to create a snapshot for a given virtual machine. It is as easy as this:
PS C:> checkpoint-vm -VMName "XP Lab"
But with PowerShell this makes it much easier to snapshot a number of machines with a single command, something you can’t really do in the graphical console.
PS C:> get-vm chi* | checkpoint-vm -SnapshotName "Weekly Snapshot $((Get-Date).toshortdatestring())" –AsJob
With this single command I’m getting all virtual machines that start with CHI and creating a snapshot called “Weekly Snapshot” that includes today’s date. Because this might take a little time to complete I’m running the tasks as background jobs.
[optin-monster-shortcode id=”lwzgfpb294pntqna”]
5. Get-VMSnapshot
If you have VM snapshots, you will most likely want to manage them. Here’s a great situation where the PowerShell cmdlet is your friend. First, what are all the snapshots I currently have?
PS C:> get-vmsnapshot *
Figure 4
You can get all snapshots for a given VM.
PS C:> get-vm "fp01" | get-vmsnapshot
Figure 5
Or a specific snapshot:
PS C:> get-vm chi-fp01 | get-vmsnapshot -Name weekly*
Figure 6
6. Remove-VMSnapshot
Of course, you will want to periodically delete old snapshots which is where Remove-VMSnapshot comes in handy. Let’s say a week or two has passed and I want to clean up the previously created snapshots. Here’s my single line PowerShell command.
PS C:> get-vm chi* | Get-VMSnapshot -Name "weekly snapshot 4/20/2013" | Remove-VMSnapshot –whatif
As you can see in Figure 7, the –WhatIf parameter allows me a chance to double-check what I’m about to do.
Figure 7
To actually remove the snapshots all I need to do is re-run the command omitting the –Whatif parameter.
7. Test-VHD
I find Test-VHD quite handy in performing periodic validation of VHD and VHDX files. This cmdlet should detect if there is a potential problem with a VHD such as if a differencing chain has been broken. The cmdlet returns True if everything is ok. You can check multiple files at once:
PS C:> dir F:VHD | test-vhd True True True True True True
Although, if one of these failed I wouldn’t necessarily know which one. Instead I might try something like this:
PS C:> dir G:VHDs | Select Name,@{n="Test";Expression={ Test-VHD $_.fullname}} | sort Test
You can see the results in Figure 8.
Figure 8
8. Export-VM
I’ve written about Export-VM in the past to export a machine to disk. This command will export the virtual machine configuration, a copy of the VHD/VHDX files as well as any snapshots. Just make sure the destination doesn’t contain a folder with the same name as your virtual machine or the command will fail. Because this can take some time to run, I recommend using the –AsJob parameter. I can export a single machine:
PS C:> export-vm "test rig" -Path E:BackupFolder –asjob
Or multiple machines:
PS C:> export-vm jdh* -Path e:backupfolder -AsJob
9. Get-VMHost
This cmdlet provides information about the server, or in my case desktop, running Hyper-V.
Figure 9
The cmdlet has a –Computername parameter so you can connect to remove Hyper-V servers.
10. Get-VMNetworkAdapter
Lastly, I find Get-VMNetworkAdapter very useful because it helps me discover the IP addresses my virtual machines are running.
PS C:> Get-VMNetworkAdapter -VMName chi-dc04
Figure 10
Or to just get the addresses for this VM, I can run a command like this:
PS C:> Get-VMNetworkAdapter -VMName chi-dc04 | Select -expand IPAddresses
Figure 11
It isn’t that much more work to display all the IP v4 addresses for my currently running virtual machines.
PS C:> get-vm | where { $_.state -eq 'running'} | get-vmnetworkadapter | Select VMName,SwitchName,@{Name="IP";Expression={$_.IPAddresses | where {$_ -match "^172."}}} | Sort VMName
Figure 12
Try doing that in GUI!
Summary
The graphical Hyper-V management console is fine for simple and one-off type tasks. But when you want to work with multiple virtual machines at once or dive deeper into a virtual machine configuration say for reporting purposes, then there really is no substitute for starting with these 10 cmdlets. I’m confident that as you work with these you’ll discover a few more cmdlets that will make your life much easier.