Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.

Essential PowerShell Cmdlets For Managing Hyper-V

Essential PowerShell Cmdlets For Managing Hyper-V

PowerShell version 5 offers IT admins a ton of Hyper-V-related cmdlets. Learn how to install Hyper-V, create new virtual machines and VM resources, set up a Virtual Switch for networking and more, all through PowerShell.

The days of buying a separate hardware server for each machine you wanted to run in your test lab are long gone. Today you just spin up a new virtual machine when you need one, or spin them up in sets to set up and then tear down entire labs. You could do this with something like VirtualBox, which is free, but it's not easy to automate. Instead, Microsoft's Hyper-V virtualization platform is easy to automate and is just another Windows feature that you can add or remove at will.

In this article you'll see that Microsoft has virtualization technology that comes with Windows Server and Windows 10, you just have to enable it. And with PowerShell it's easy to work with Hyper-V Virtual Machines and all of their components in the command line. This allows IT admins the ability to automate tasks easily for any number of VMs.

Let's start with a quick look at how to get Hyper-V set up on your machine and get a VM created and connected to the outside world. 

MORE: Is Configuration Management Right For You?
MORE: Finding The Right Configuration Management Tool

MORE: PowerShell Tutorials

Installing Hyper-V

Hyper-V is just a feature of Windows. If you are running Windows Server you can see that the feature is already there, you just have to enable it. Run this script to find it:

Get-WindowsFeature Hyper-V

If you are running Windows 10 and have hardware that can handle it, you can enable Hyper-V too. You can find it using this script:

Get-WindowsOptionalFeature -FeatureName "Microsoft-Hyper-V*" -online | Format-Table

To enable Hyper-V on Windows Server, use the short script below:

Install-WindowsFeature Hyper-V –Restart

If you don't want your machine to immediately restart, just get rid of the -Restart parameter.

To enable Hyper-V on Windows 10, use the following script:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

Note: There is a potential gotcha that you should be aware of when running these scripts, especially if you are trying to run them on older hardware. Even if your processor will support the required virtualization features, they may simply not be enabled in the BIOS. Windows Server will warn you immediately, but Windows 10 may wait to say something until you attempt to add a VM. If you get error messages that you can't do something while trying to follow along, make sure to look up the documentation for your computer's processor, and check to ensure your computer's BIOS has all of its virtualization features enabled.

If you just installed the Hyper-V role on Windows Server you still need to install the Hyper-V PowerShell Module using the script below:

Add-WindowsFeature RSAT-Hyper-V-Tools -IncludeAllSubFeature

If you are on Windows 10, the Enable-WindowsOptionalFeature script you ran above has already installed it. Either way you will need to restart your computer. 

If you had to change BIOS settings, or if you have to do so later, make sure you fully power off, and not just restart through the operating system to ensure the changes take effect for the processor. 

Finally, make sure you update all of your help files since you just installed a new PowerShell module and you want to make sure you have the latest help files:


Creating Virtual Machine Resources

A virtual machine that can connect to the outside world will consist of the following components:

  1. A virtual machine - This is a space to actually run the guest operating system
  2. Boot media - For this exercise we will use an Ubuntu ISO just because it's easy
  3. A virtual DVD drive to mount the iso to the VM
  4. A virtual disk to install the guest operating system into and save changes
  5. A virtual network adapter - This is pretty literally a virtual version of a network card that you would install into a server
  6. A virtual network switch - The virtual switch is what the virtual network card connects to and provides a path for data to get to and from the guest operating system through the host

First we are going to create a couple directories to hold the files we need. Later when you get comfortable with creating VMs you can start to put files wherever works best for you.

New-Item -ItemType directory -Path ("c:\vm","c:\vm\iso") -Force

Getting the Ubuntu install media is easy. We're using Ubuntu Desktop. Download the ISO and put it in c:\vmiso

We will continue laying the groundwork by creating a virtual switch before we create the virtual machine. To create the virtual switch, we need to know the name of the network adapter it will connect to with the host operating system. You probably created it a long time ago and forgot the name, but don't worry, there's an easy way to find the name you need with the script below.

Conveniently we only have one. The name is all you'll need to create the switch as shown below.

The switch we just created allows the VMs to communicate with the outside world because its what's called an "external" switch, even though really it's virtual and only exists in this machine. We can also create a switch that can only connect our VMs to one another. That would be called a "private" switch and we would create it like this.

Combining these two kinds of switches we can create configurations such as a group of machines in a virtual intranet with a single VM acting as a gateway.

Next we'll create the virtual hard disk as a fixed size vhdx file.

That command will take a little time so you'll probably see a progress bar.

MORE: How To Use PowerShell DSC To Build A Hyper-V Virtual Machine
MORE: Intro To PowerShell Desired State Configuration (DSC)

Creating VMs

To create a Hyper-V VM takes just one short command as shown below.

The command above referred to the vhdx we created earlier, which was a pre-sized disk. If you are low on drive space you could also bypass creating a fixed size disk and create a VM first with a dynamically sized disk that would grow as you used it. The command for dynamic sized Hyper-V VMs is show below.

For this demo we will continue with the first machine and the fixed-size disk, but we aren't going to start it just yet. If we start it right now we won't have any way to boot an operating system or connect to the internet.

To add a boot device, we will map the ISO to a virtual DVD drive using this command:

Add-VMDvdDrive -VMName "Ubuntu Desktop" -Path .\iso\ubuntu-16.04-desktop-amd64.iso

If you forget to do that first and start the VM you will not only get a boot failure, but you will have to shut down the VM before you can add the drive. Attempting to add it while the VM is running will only result in an error.

We could stop here and boot the machine, but we'll connect the network adapter first to make sure we can connect to the internet. In order to create the network adapter, we need to know the name of the switch we created earlier. If you don't remember the name that's no problem, we can find any virtual switches on the machine as shown below.

Since this is PowerShell, to connect the adapter to the switch we can use the Pipeline. First we'll look at the VM we just created and find the network adapters. There should only be one.

Having confirmed there's only one, we can pipe it to the next Cmdlet to get it connected to the switch and out to the Internet.

You won't get any return from this script so to confirm that it works, just run the first part of it again.

The SwitchName property tells us that the virtual adapter is now connected to the virtual switch.

Starting And Working With VMs

With the network adapter connected and the boot disk in place we can now start the VM. You can start it with the Start-VM Cmdlet:

Start-VM -Name "Ubuntu Desktop"

Before we connect to the VM though, let's look at some of its properties in PowerShell and confirm that everything is working as expected. The screenshot below shows one way to do that.

We can see the machine is running and now has an uptime number, and the network adapter has a MAC address.

To connect to the VM and see what it looks like, click on the start menu, type Hyper-V, and click on the Hyper-V Manager.

Once in the Hyper-V Manager, right click on your new VM and click connect as shown below.

When you connect, the boot disk has probably had a minute to get the machine started, so you should see something like this:

If you continue with the installation the VM will eventually want to reboot, and you will probably get the same error message that you would if you rebooted a physical machine and forgot to remove the boot disk. The VM will complain that OS installation is complete and you need to remove the install media. 

Removing the virtual disk is easy. Just execute the script below:

Stop-VM "ubuntu desktop" -Passthru | Get-VMDvdDrive | Remove-VMDvdDrive

Before we restart the VM let's create a checkpoint just to show how easy it is.

First we checkpoint the VM and create a unique name for it so it's easy to get back to exactly that spot.

Get-VM "ubuntu desktop" | Checkpoint-VM -SnapshotName 'Starting Point'

Make any changes you like and then run the following script to revert the machine back to the snapshot.

Get-VM "ubuntu desktop" | 
    Get-VMSnapshot -Name 'Starting Point' | 
        Restore-VMSnapshot -Confirm:$false

Since those Cmdlets all accept and pass pipeline input, you can run that same command to checkpoint and restore any number of VMs you like. You can run a demo on a ten server environment, and then restore them to a check point and run the demo all over again.

If you want to read more about working with Hyper-V in PowerShell you can start with this TechNet reference to all of the Hyper-V PowerShell Cmdlets. You could also read about the xHyper-V DSC Module which is an excellent way to set up entire labs of VMs with a simple declarative syntax.

MORE: Essential PowerShell Cmdlets for Managing Storage
MORE: Essential PowerShell Cmdlets for Managing Active Directory
MORE: Essential PowerCLI Cmdlets for Managing VMware Virtual Machines