Use PowerShell to Set Up a Hyper-V Lab

A Hyper-V lab for your PowerShell sandbox

When learning and playing with new technology like PowerShell, a lab environment is extremely beneficial. Some of the benefits are:

A lab environment can be torn down and and rebuilt as needed.

Components can be added to or removed from a lab as needed.

Actions that are considered risky to perform in a production environment can be performed safely in a contained lab.

Windows 10 Pro, Enterprise, and Education (as well as Windows 8 Pro and Enterprise) come with the same Hyper-V technology that runs in the datacenter, just waiting to be enabled. Here is how to get it installed and configured using PowerShell.

Hyper-V Requirements

To be clear, not ANY Windows 8 or 10 computer can run Hyper-V. There are Windows edition requirements as well as hardware requirements on the system itself.

Windows Edition Requirements

Windows 10

Professional

Enterprise

Education

Windows 8

Professional

Enterprise

If you have a Home edition of Windows, I would highly recommend an upgrade to Pro. The upgrade price is around $99, which is less than you would pay for VMware Workstation, and with Hyper-V you get the benefits of PowerShell management capabilities.

Hardware Requirements

Data Execution Prevention

Second Level Address Translation

Virtualization Enabled in Firmware

VM Monitor Mode Extensions

You’re probably wondering how to go about verifying these requirements on your computer? First of all, any modern, reasonably outfitted (Intel or AMD) desktop or laptop is going to meet these requirements, just steer clear of the extreme low-end budget laptops. If you are checking your own computer or can get into a PowerShell prompt on a display model at a store, run the following command. If all of the values are True, then the computer will support Hyper-V.

Hyper-V requirements

PowerShell

1

2

3

4

5

6

7

8

9

10

PSC:\Users\aaron>Get-ComputerInfo-PropertyHyperVRequirement*|fl

HyperVRequirementDataExecutionPreventionAvailable:True

HyperVRequirementSecondLevelAddressTranslation:True

HyperVRequirementVirtualizationFirmwareEnabled:True

HyperVRequirementVMMonitorModeExtensions:True

Note that Get-ComputerInfo is only in PowerShell 5.1 or newer, so if you are an older version of Windows, you can run msinfo32 and on the Summary page verify the Hyper-V values all say “Yes”.

msinfo32 output for Hyper-V support.

Adding Hyper-V Windows Features

The Hyper-V components and management tools are already part of the OS, available as optional Windows features. To check the current install status of these features, run the following PowerShell from an elevated prompt. By default, these features will be disabled.

Create VM network switches

Before we create a VM, we want to create VM network switches to provide networking connectivity. We will create four different switches which are capable of providing different types of network connectivity: External, Internal, Private, and NAT.

External VM switch

Here is how to create the External VM switch using PowerShell. Note that we get the host adapter name to bind the switch to using the adapter name “Ethernet” as it appears in Network Connections. You may need to change this based on your own configuration.

Create your first VM

With networking switches provisioned, we can proceed with creating our first VM. We will create the VM, add a DVD drive to the VM, and mount our Install Media as an ISO.

Create the VM files and virtual disk

The entire block of commands is broken up here, but note that the variables we set at the beginning are used throughout. The install media we are going to use is the Windows Server 2016 Evaluation, which can be downloaded at the Microsoft Technet Evaluation Center.

Back to the GUI – Installing Windows

To continue with our lab setup, we need to return to the GUI. In your start menu, find the desktop app Hyper-V Manager and open it. This is an mmc snap-in, so you can also run mmc and add the snap-in.

desktop app

You will see a your new VM. To open up the KVM console to the VM, right-click the VM name and select Connect… Power it on, and install Windows like you normally would.

Create a snapshot to save the VM state

So you’ve installed Windows, activated your license, set your Administrator password, installed all available updates, and made any other customization you typically make. In order to preserve this pristine state of the VM, we create a Checkpoint (aka a Snapshot). A checkpoint is a point-in-time recovery point. Once you create a checkpoint, any new or overwrite changes to the virtual hard disk are saved to a delta file. If you Apply the checkpoint, it will merge the delta changes with the original virtual hard disk. If you Revert to the checkpoint, everything in the delta file is in essence deleted and you are returned to the original virtual hard disk and the time the checkpoint was taken.

Create a VM checkpoint

Create VM checkpoint

PowerShell

1

2

3

4

Checkpoint-VM-Name"DemoVM"-SnapshotName"Pristine State"

See existing checkpoints for a VM

PowerShell

1

2

3

4

5

6

7

8

PSC:\WINDOWS\system32>Get-VMCheckpoint-VMName"DemoVM"

VMName Name SnapshotType CreationTime ParentSnapshotName

----------------------------------------------------

DemoVM Pristine State Standard10/10/20169:49:30PM

Revert to a VM checkpoint

When you are done with a sandbox session, you want to revert back to the pristine state so something you did during this session doesn’t conflict with a future session (and you don’t remember what you had previously done). Be aware that this will power off your VM during the process.

Next Steps

Now that you know how to get the foundation configured for a Hyper-V lab:

Install and configure one on your own workstation! If you don’t have enough storage, consider using a USB hard drive for housing your ISOs and VHDs. If your computer is lacking on the CPU and Memory side, consider getting a desktop to use as a lab system and Remote Desktop to it.

Explore all of the cmdlets that are part of the Hyper-V module. Start by running Get-Command -Module Hyper-V | Out-GridView for a filterable list of all available cmdlets. Then run Get-Help <cmdlet name> to find out more about what it does.