Awesome Hyper-V Cmdlets

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.

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

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.

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.

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.

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.

The other option you have is to stop the virtual machine but put it in a saved state.

4. Checkpoint-VM

Use the Checkpoint-VM to create a snapshot for a given virtual machine. It is as easy as this:

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.

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.

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?

Figure 4

You can get all snapshots for a given VM.

Figure 5

Or a specific snapshot:

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.

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:

Although, if one of these failed I wouldn’t necessarily know which one. Instead I might try something like this:

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:

Or multiple machines:

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.

Figure 10

Or to just get the addresses for this VM, I can run a command like this:

Figure 11

It isn’t that much more work to display all the IP v4 addresses for my currently running virtual machines.

DevOps Consultant. I’m strongly focused on automation, security, and reliability.