Windows 8 includes native PowerShell cmdlets for Hyper-V for the first time ever. You can use this to manage Hyper-V from the command prompt and to automate complex and/or repetitive tasks. In fact, Hyper-V appears to be going down the Exchange route where only so much is revealed in the GUI, and everything is in PowerShell. I’d expect System Center Virtual Machine Manager to provide that additional GUI functionality (and use PowerShell under the hood).
But before you even get to doing Hyper-V cmdlets, you’ll need to enable the Hyper-V role. One could use Server Manager, but we’ll use the Server Manager cmdlets instead. You can learn lots more about this cmdlets in the chapter that I wrote on Server Manager in Mastering Windows Server 2008 R2.
PowerShell cmdlets are a subset of modules. To use a cmdlet you have to load the relevant module. Which one? You can run a cmdlet to find that out:
You’ll notice that PowerShell works in the format of Verb–Noun (action-thing to manage) with optional flags. In this case it’s retrieving the list of all available modules that can be imported.
Then I can run the following to import the Server Manager module to get access to the relevant cmdlets:
Try this: type in half of ServerManager and press <TAB>. This should autocomplete the text for you.
If I now run the following cmdlet then I get a list of all imported modules:
Note: that if I knew what I was doing, I would have just run the Import-Module cmdlet to import ServerManager.
What cmdlets are not available to me? There’s a cmdlet for that. I can run Get-Command but that lists every available cmdlet. I want to see the ones in the ServerManager module:
Get-Command –Module ServerManager
It turns out that I have:
- Get-WindowsFeature: list what features and roles are available, and what their install status is (indicated by an X).
- Install-WindowsFeature: install a feature or role
- Uninstall-WindowsFeature: install a feature or role
Need some help? That’s built in too:
We want to know what the state of the machine is so we can run
This spits out a list of every role, role service and feature that you can install or uninstall via Server Manager. You should note the name(s) of the ones you want to work with because you you’ll need them for the next step.
When you check the help of Install-WindowsFeature you’ll see some interesting flags:
- -Whatif: It doesn’t run the cmdlet, but it simulates it. It will tell you (to the best of its ability) if the command will work or not.
- -Restart: forces and automatic reboot to happen, rather than prompting for it. I know that enabling Hyper-V requires a reboot so this is useful.
For example this will simulate the install of the Hyper-V role:
Install-WindowsFeature Hyper-V –Restart –Whatif
If I’m happy, and I know a reboot of the new host is OK then I can run:
Install-WindowsFeature Hyper-V –Restart
A progress bar will appear in the PowerShell window, and the new host will reboot so the Hyper-V role can be enabled and started up.
That sounds like we’d done a lot of cmdlet-ing? We actually could have consolidated it into two commands or a 2 line script:
Install-WindowsFeature Hyper-V –Restart
That simple .PS1 script could have been stored in a file share, attached to a Windows deployment, or used in many different ways to automate a Hyper-V enablement. And that’s just the tip of the iceberg.
What if you want to uninstall Hyper-V? Well that uninstall is just like the install:
Uninstall-WindowsFeature Hyper-V –Restart –Whatif
Hyper-V is now up and running, and ready to manage. Sure, you can use the Hyper-V console, and to be honest, that is my primary tool. But many will want to use PowerShell for automation. Here’s how you can get started:
Import the modules with:
And list the cmdlets:
Get-Command –Module Hyper-V
There’s lots there to get playing with!
This blog post is the property of Aidan Finn (@joe_elway / http://www.aidanfinn.com) and may not be reused in any manner without prior consent of Aidan Finn. You may quote one paragraph from this blog post if you link to the original blog post.
No related posts.