Installing Powershell

In order to manage Hyper-V from the command line, you will need Powershell. You can install it both on your Hyper-V server (to start testing on the local machine) and on your local PC (so that you can run Powershell commands to the server remotely). In my case, I just installed it on the server as that is where I will be demonstrating the Hyper-V Powershell commands.

To install Powershell in Windows 2008, go to Server Manager, then to Features. Click Add Features. Check the Windows Powershell box and click Next.

Powershell Managing Hyper-v_Powershell
Figure 1
: Installing the Windows Powershell Feature

Now, click Install to actually install it.

When done, you should see Installation Succeeded and you can click Close.

Now when you go to Start - All Programs, you should see Windows Powershell and you can click on Windows Powershell underneath that.

Powershell Managing Hyper-v_Powershell_02
Figure 2: Starting Powershell’s command line tool

At this point, you should have the distinctive Windows Powershell dark blue command line window up, like this:

Powershell Managing Hyper-v_休闲_03
Figure 3: Windows Powershell Interface

Before you actually move on to running commands, there is something else that you really want to get – the Hyper-V Powershell Library.

Downloading and Installing the Hyper-V Powershell Library

James O’Neill is the developer of the Powershell management library for Hyper-V and his library is invaluable when it comes to administering Hyper-V from the command line. To download and install it, go to the Powershell management library website above and get the Hyperv.zip file. It contains 2 files inside. Extract these to a simple directory you can get to easily at the command line, like C:\temp or make a directory like C:\powershell.

Now, Start PowerShell. Change directory into the directory with the Hyper-V PowerShell Library scripts.

Unless you want to get into the digital signing of PowerShell scripts you can disable PowerShell security temporarily by doing:

Set-ExecutionPolicy unrestricted

Then, to load this new Hyper-V PowerShell Library you need to do this:

. c:\temp\hyperv.ps1

Notice the “dot” then “space” and then the full path to the Hyper-V PS1 file. That “dot space” is very important because that is what keeps this library in memory for the duration of this PowerShell session.

From here, you will be given a security warning an you must choose “R” to run this script. You should be rewarded with “VM Functions Loaded” and a list of all the commands that you are now enabled to run so that you can manage your Hyper-V Server from the PowerShell command line.

Here is what it looks like:

Powershell Managing Hyper-v_Powershell_04
Figure 4: Installing the Hyper-V PowerShell Library

Now that our library is installed, let us find out how to use it.

Administering Hyper-V using the PowerShell Library

The Hyper-V PowerShell Library User Guide explains how to use the, almost 100, commands included in the library. And remember that each of these almost 100 commands has many parameters. Thus, this is a huge management library for Hyper-V, not just a handful of commands. Explaining how to all the commands goes beyond the scope of this article (and that is why there is a 41 page user guide).

That being said, let me show you how to use a handful of the basic commands in the Hyper-V PowerShell Library.

And keep in mind that, just with any commands you can perform at the command line, they become exponentially more powerful when combined with scripts that can use them intelligently.

Here are 5 Hyper-V PowerShell Library Commands and how to use them:

  • Get-VMsummary – provides a detailed summary of all guest virtual machines on your Hyper-V server

Powershell Managing Hyper-v_Powershell_05
Figure 5: Get-VMsummary output

  • Start-VM, Stop-VM, Suspend-VM, and Shutdown-VM – the purpose of these commands are obvious. All you need to provide is the VMElementName as it was shown in Figure 6.

Powershell Managing Hyper-v_Managing_06
Figure 6: Using the Start-VM command

  • New-VM – what is the quickest way to create a new guest VM? Just type New-VM and here is what you get, a new virtual machine that you still need to configure and start.

Powershell Managing Hyper-v_Managing_08
Figure 7:
Creating a new virtual machine

  • Get-VMmemory lists out all virtual machines on the host and their memory amount.

Powershell Managing Hyper-v_Powershell_09
Figure 8: Get-VMmemory

  • Get-VMsnapshot

The Get-VMsnapshot command shows all snapshots created in your Hyper-V environment. When it comes to snapshots, you can also use commands like Update-VMsnapshot, New-VMsnapshot, Apply-VMsnapshot, Get-VMsnapshotTree, and Choose-VMsnapshot.

Powershell Managing Hyper-v_职场_10
Figure 9:
Get-VMsnapshot

Special Thank you

I want to offer a special thank you to Mark Wilson and James O’Neill.

The idea for this post came from Mark Wilson’s video covering managing Hyper-V from the command line. For more information on Mark, visit Mark’s Blog.

James O’Neill is the developer of the Powershell management library for Hyper-V and his library is excellent! You can learn more about James at his blog.

Conclusion

In conclusion, managing Hyper-V from the command line is essential for IT Admin. Doing it with PowerShell just gives you even more power. While you can manage Hyper-V with PowerShell without the PowerShell Management Library – who would want to? James O’Neills’s Powershell management library for Hyper-V is a huge gift for all of the virtualization admins who want to use PowerShell and Hyper-V. I encourage you to try this for yourself – it’s fun and easy!