More PowerShell in R2 tricks

The other day, Ben Armstrong posted the steps for installing Windows PowerShell on Hyper-V Server 2008 R2.  How awesome is that?  A real, modern, powerful shell on Hyper-V Server!

It should be noted that this also works on Windows Server 2008 R2 Core SKUs, so if you’re running Hyper-V on one of those, you can also use this trick.

So why is this cool?

Well, first of all, if you install PowerShell on a Server Core SKU, you can use a new and more powerful tool for installing and configuring server roles and features.

To do this, you need to load the Server Manager module into your shell with the following command:

Import-Module ServerManager

You can get a list of the new cmdlets that this module provides by running:

Get-Module ServerManager

So, if you’re on a Windows Server SKU, you can install all of your roles or features this way.  To install Hyper-V, you would use this command:

Add-WindowsFeature Hyper-V

Ok, that’s cool and all, but it’s not that spectacular.  So how about being able to actually interact with Hyper-V from this shell?  Even on Server Core or Hyper-V Server 2008 R2?

Check out James O’Neill’s PSHyperV project over on CodePlex.  If you download this library, you can interact with Hyper-V directly from your Core console. 

The first thing to remember is that you need to dot-source the HyperV.ps1 script so that all of the functions remain in memory, like so:

. .\hyperv.ps1

After you do that, you’ll get a list of all of the new functions and filters that this library provides.  Just as an example, you can create and configure a VM with these commands:

$vm = New-VM -name “MyVM”
Set-VMMemory -vm $vm -memory 2GB
Set-VMCPUCount -vm $vm -CPUCount 2
# some other logic to create a hard drive, or attach an existing one.
Start-VM -vm $vm -wait