Windows PowerShell: Create Hyper-V virtual machines

You can use Hyper-V and Windows PowerShell 3.0 to easily create, configure and manage your virtual machines.

Neil Tucker

One of the best new features in Windows 8 and Windows Server 2012 is Hyper-V version 3.0. Not being restricted to Virtual PC (with no support for 64-bit OSes) will open up many new possibilities, especially for those working on the Windows 8 client. If you need to work with Hyper-V virtual machines (VMs) on a regular basis, being able to run them on the same desktop where you do everything else is a big reason to upgrade to Windows 8. You’ll also benefit from being able to use 64-bit images on a client desktop.

You can now use a native Windows PowerShell 3.0 module to configure and manage your VMs. This is new to Hyper-V 3.0. To do this for Hyper-V 2.0 on Windows Server 2008, you had to download the Windows PowerShell module from CodePlex. You can find detailed steps on doing this in one of my past TechNet Magazine articles, “Create Hyper-V Virtual Machines with Windows PowerShell,” which will show you how to create VMs using the Windows PowerShell 3.0 Hyper-V module on Windows 8 or Windows Server 2012.

In developing a Microsoft Learning course on this subject, I created a Windows PowerShell script designed to help you create VMs. I’ll use a simplified version of that script to show how you can quickly build your own. I’ll also look at ways of getting better performance from the VMs by properly using some of the configuration options available. First, you need to install the Hyper-V role on the system.

Configure Hyper-V

Follow these steps to install the Hyper-V role on either Windows 8 or Windows Server 2012. The system must use a 64-bit processor, support hardware-assisted virtualization and hardware-enforced Data Execution Prevention (DEP). You’ll have to perform these steps from a Windows PowerShell console opened with full administrator credentials:

  1. Run the command Enable-WindowsOptionalFeature –Online –FeatureName Microsoft-Hyper-V (or Dism /Online /Enable-Feature /FeatureName:Microsoft-Hyper-V /All).
  2. Verify that there were no errors.
  3. Shut down and restart the computer.
  4. Log back on to the system with an administrator account.

The Hyper-V Windows PowerShell module is installed when the role is configured. To use the cmdlets, add the module to a Windows PowerShell console (Import-Module Hyper-V). Then you’ll have to verify that the cmdlets are available (Get-Command –Module Hyper-V). Before using any cmdlets, verify that the “Hyper-V Virtual Machine Management” service is running (Get-Service VMMS). You should execute all Hyper-V Windows PowerShell cmdlets with administrator privileges.

Configure your VMs

The script shown in Figure 1 creates and configures two VMs. Using variables makes it easier to customize and standardize the process. To automatically install the OS, create an autounattend.xml file on the disk the OS ISO image will use. If you won’t be using this installation method, remove the Set-VMDvdDrive and Set-FloppyDiskDrive cmdlets. You can then use New-VM to create each VM. This script will also create a DVD and floppy drive, even if they aren’t designated.

Figure 1 This script will help you create and configure two VMs.

# This script configures the Hyper-V machines used for the 50331 Course.
# PowerShell 3.0 and Windows Server 2012 or Windows 8 Pro are required to perform this setup.
# The C:\ Drive should have at least 200GB of free space available.
# All the files on the 50331 Student CD should be copied to C:\Labfiles before performing this setup.

# Variables
$CLI1 = "50331-CUSTOM-CLI"		# Name of VM running Client Operating System
$SRV1 = "50331-CUSTOM-SRV"		# Name of VM running Server Operating System
$CRAM = 2GB				                # RAM assigned to Client Operating System
$SRAM = 1GB				                # RAM assigned to Server Operating System
$CLI1VHD = 80GB				                # Size of Hard-Drive for Client Operating System
$SRV1VHD = 40GB				                # Size of Hard-Drive for Server Operating System
$VMLOC = "C:\HyperV"			        # Location of the VM and VHDX files
$NetworkSwitch1 = "PrivateSwitch1"	# Name of the Network Switch
$W7ISO = "C:\Labfiles\Windows7.iso"	# Windows 7 ISO
$W7VFD = "C:\Labfiles\Windows7.vfd"	# Windows 7 Virtual Floppy Disk with autounattend.xml file
$WSISO = "C:\Labfiles\W2K8R2.iso"	        # Windows Server 2008 ISO
$WSVFD = "C:\Labfiles\W2K8R2.vfd"	# Windows Server 2008 Virtual Floppy Disk with autounattend.xml file

# Create VM Folder and Network Switch
MD $VMLOC -ErrorAction SilentlyContinue
$TestSwitch = Get-VMSwitch -Name $NetworkSwitch1 -ErrorAction SilentlyContinue; if ($TestSwitch.Count -EQ 0){New-VMSwitch -Name $NetworkSwitch1 -SwitchType Private}

# Create Virtual Machines
New-VM -Name $CLI1 -Path $VMLOC -MemoryStartupBytes $CRAM -NewVHDPath $VMLOC\$CLI1.vhdx -NewVHDSizeBytes $CLI1VHD -SwitchName $NetworkSwitch1
New-VM -Name $SRV1 -Path $VMLOC -MemoryStartupBytes $SRAM -NewVHDPath $VMLOC\$SRV1.vhdx -NewVHDSizeBytes $SRV1VHD -SwitchName $NetworkSwitch1

# Configure Virtual Machines
Set-VMDvdDrive -VMName $CLI1 -Path $W7ISO
Set-VMDvdDrive -VMName $SRV1 -Path $WSISO
Set-VMFloppyDiskDrive -VMName $CLI1 -Path $W7VFD
Set-VMFloppyDiskDrive -VMName $SRV1 -Path $WSVFD
Start-VM $SRV1
Start-VM $CLI1

Using the parameters for New-VM, you can give each image a name, location, memory allocation, hard drive and network connection. You must define the virtual network switch (New-VMSwitch) before creating a network connection on the VM. The Hyper-V environment can support multiple virtual network switches with the same name, so verify that your chosen network switch doesn’t already exist using the $TestSwitch variable before you create a new one.

You can further configure your system’s memory and hard drive to improve performance. Use Set-VMMemory to configure dynamic memory settings (for example, you can assign minimum, startup and maximum memory settings). This is very useful on systems that support multiple images.

Use the New-VHD cmdlet to create a fixed or dynamic disk, to use base and differencing drive options, or to manage block and sector sizes. Dynamic disks are the default. You can create these quickly, but fixed disks provide better performance.

Regardless of which options you use to configure the hard drive, you should take advantage of the new VHDX format. You might not need a 64TB hard drive (the Virtual Hard Disk, or VHD, format is limited to 2TB), but the new logging capabilities mean fewer errors if there’s a system or power failure.

Microsoft documentation indicates that VHDX drives can run faster than VHDs. (I confirmed this in tests I ran on two computers.) You can also convert existing VHDs to VHDX or vice versa using Convert-VHD or the Hyper-V Manager.

As you can see, using this script to create VMs with Windows PowerShell is relatively easy and requires only a few cmdlets. For anyone that works with images on a server or client platform, this opens up new possibilities when managing a virtual testing, security or deployment environment.

Neil R. Tucker

Neil R. Tucker is an MCT with years of experience teaching Windows PowerShell, SQL Server and Windows OS classes. He’s the author of the Microsoft Learning 50331 Course, written for Windows 7 Desktop Support Technicians. He can be reached through his Web site at