10 Awesome Hyper-V Cmdlets 10 Awesome Hyper-V Cmdlets

07 May by Jeffery Hicks     5     Hyper-V & PowerShell

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.

get vm hyper-v cmdlet syntax

Figure 1

Remember that what you are seeing are virtual machine objects and there is more than this default display.

hyper-v cmdlet get vm default

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.

hyper-v cmdlet get vm filter

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.

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.

Have any questions?

Leave a comment below!

 

Backing up Hyper-V

If you’d like to make backing up your Hyper-V VMs easy, fast and reliable, check out Altaro Hyper-V Backup v4. It’s free for up to 2 VMs and supports Hyper-V Server 2012 R2! Need more? Download a 30-day trial of our Unlimited Edition here: http://www.altaro.com/hyper-v-backup/. 

Receive all our free Hyper-V guides, checklists and ebooks first via email!

Thank you, your sign-up request was successful! Please check your e-mail inbox.
Given email address is already subscribed, thank you!
Please provide a valid email address.
Oops. Something went wrong. Please try again later.
(Don’t worry, we hate spam too!)

Jeffery Hicks

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and is a frequent speaker at technology conferences and user groups. His latest book is PowerShell In Depth: An Administrator's Guide 2nd Ed.

5 Responses to “10 Awesome Hyper-V Cmdlets”

  1. James Stephan

    Hey, I am a fan of your posts and I just want to point you to my blog and a couple of powershell scripts I have created them from a hodge podge of sites and just trial and error, not a programmer just old school administrator

    http://stephanco.blogspot.com/2013/05/detailed-report-of-your-hyper-v-server.html this creates text file report of the vhost and vm’s

    http://stephanco.blogspot.com/2013/05/new-vm-powershell-commandlet-to-easily.html this is an easy way to create vm’s from powershell, easy to use and easy to understand

  2. James Stephan

    I created a new project called pshvm.codeplex.com and it is the first all powershell script free hyper-v manager that includes a replacement for vmconnect and mstsc. It is meant to be run from the console. I included my reporting.codeplex.com project so now you can manage and report for free. It can even create VM’s with just a few clicks and answering a few questions.

  3. Darren

    Hi. Am looking to script to list all VMs in SC Virtual Machine Manager, and their Maximum disk size but struggling to get this right.

    This is my current thinking, but need guidance please…
    get-vm | select name,$_.VirtualHardDisk.MaximumSize

    Thanks

    • Jeffery Hicks
      Jeffery Hicks

      I don’t have SCVMM so I don’t know if those cmdlets are a little different. Using they Hyper-V module, I can do something like this.

      Get-VM | Select Name,@{Name=”MaxSizeGB”;Expression={ (Get-VHD $_.id)[0].Size/1GB}}

      This will get the first hard drive for each VM, which is usually the primary drive. Or if you wanted to enumerate all drives you might try something like this:

      Get-VM CHI* | Foreach {
      $_.HardDrives | Select VMName,Path,Controller*,@{Name=”MaxSizeGB”;Expression={ (Get-VHD $_.path)[0].Size/1GB}}
      }

      That said, if by chance the Get-VM cmdlet you are using with SCVMM is a little different, see if this gets you closer. I’m making an educated guess as to the syntax.

      get-vm | select name,@{Name=”MaximumSize”;Expression={ $_.VirtualHardDisk[0].MaximumSize}}

      I’m assuming the VirtualHardDisk property is a collection of disk objects and that you want the first one.

Leave a comment