Create Hyper-V machines via Powershell

If you need to create and configure virtual machines on a regular basis, using Windows PowerShell to do so can speed up the process.

Configure Windows PowerShell

This strategy assumes you’re working on a Windows Server 2008 R2 system that meets the requirements for the Hyper-V role. After following these steps, the system will be ready for Hyper-V scripting:

  1. Install the Hyper-V role and Windows PowerShell. You can do this from the Server Manager or by using the ServerManagerCmd.exe command-line tool (ServerManagerCmd.exe –install Hyper-V and ServerManagerCmd.exe –install PowerShell).
  2. Configure Windows PowerShell to allow the use of scripts. This is disabled by default. You can configure this with the Set-ExecutionPolicy cmdlet (Set-ExecutionPolicyRemoteSigned).
  3. Download and install the Windows PowerShell Hyper-V module. You need this module to access all the cmdlets designed specifically for Windows PowerShell. From the CodePlex PowerShell Management Library for Hyper-V page, download the latest stable, non-development release. Make sure the “block” attribute is removed from the zipped files before you install them. This will execute setup scripts without needing digital signatures. If you’re doing the install on Server Core, unblock and unzip the module file on another computer before copying the setup files to the server. Otherwise, you’ll need additional tools to perform these operations on the server (the stream.exe tool on SysinternalsSuite and 7-Zip).
  4. From an elevated Windows PowerShell command prompt, enable the Hyper-V cmdlets by importing the newly installed module (Import-Module HyperV). If you get an error message, verify that you didn’t miss a previous step. If you intend to regularly use Windows PowerShell to manage your Hyper-V environment, you should add the Import-Module and Set-ExecutionPolicy cmdlets to a Windows PowerShell profile file. This will let you use the cmdlets without having to configure the server each time. You should always use an elevated Windows PowerShell command environment to avoid any issues running the cmdlets.

To verify you’ve successfully installed the cmdlets and they’re functioning properly, use the Get-Command cmdlet to see a list of the commands available to you (Get-Command –Module Hyperv). As with other modules, each cmdlet comes with helpful information about its functions and examples on how to use it (Get-Help New-VM –Detailed and Get-Help New-VM –Examples). There’s additional documentation available on CodePlex.

Configure the Hyper-V Windows PowerShell Script

Once your Windows PowerShell environment is ready, you can start building new VMs. Make sure you start with Administrator permissions to use elevated privileges for these commands. The script uses Hyper-V cmdlets to create a new VM based on five variables (see Figure 1) you provide during setup. Each variable has a pre-assigned default value that will be used if one isn’t provided.

Figure 1 Descriptions of the variables defined during Hyper-V virtual machine (VM) setup.

VM name

Amount of memory assigned to the VM

Size of the virtual hard drive the VM is using

Location of where you want to create the VM virtual hard drive

VM virtual network connection

After defining those variables, the New-Image.ps1 script (see Figure 2) configures the Hyper-V Virtual Network using the value assigned to the $Network1 variable. Before defining the new private network with the New-VMPrivateSwitchcmdlet, remove it with Remove-VMSwitch, whether or not it already existed. This ensures you don’t define duplicate networks with the same name.

Figure 2 The New-Image.ps1 script that creates new virtual machines.

# This script creates a new Hyper-V machine with hard drive, memory & network resources configured.

# Variables
$SRV1 = Read-Host "Enter the Virtual Machine name (Press [Enter] to choose Server01): "
if ($SRV1 -eq ""){$SRV1="Server01"} ; if ($SRV1 -eq $NULL){$SRV1="Server01"}

$SRAM = Read-Host "Enter the size of the Virtual Machine Memory (Press [Enter] to choose 512MB): "
if ($SRAM -eq ""){$SRAM=512MB} ; if ($SRAM -eq $NULL){$SRAM=512MB}

$SRV1VHD = Read-Host "Enter the size of the Virtual Machine Hard Drive (Press [Enter] to choose 40GB): "
if ($SRV1VHD -eq ""){$SRV1VHD=40GB} ; if ($SRV1VHD -eq $NULL){$SRV1VHD=40GB}

$VMLOC = Read-Host "Enter the location of the Virtual Machine file (Press [Enter] to choose C:\HyperV): "
if ($VMLOC -eq ""){$VMLOC="C:\HyperV"} ; if ($VMLOC -eq $NULL){$VMLOC="C:\HyperV"}

$Network1 = Read-Host "Enter the name of the Virtual Machine Network (Press [Enter] to choose Network1): "
if ($Network1 -eq ""){$Network1="Network1"} ; if ($Network1 -eq $NULL){$Network1="Network1"}

# Configure Hyper-V Virtual Network
remove-vmswitch $Network1 -force -erroractionsilentlycontinue
new-vmprivateswitch $Network1

# Create Virtual Machines
MD $VMLoc -erroractionsilentlycontinue
new-vm $SRV1 -path $VMLoc
new-vhd -vhdpaths $VMLoc\$SRV1 -size $SRV1VHD
add-vmdisk -vm $SRV1 -controllerid 0 -lun 0 -path $VMLoc\$SRV1
get-vm $SRV1 | add-vmdrive -controllerid 1 -lun 0 -dvd
get-vm $SRV1 | set-vmmemory -memory $SRAM
get-vm $SRV1 | add-vmnic -virtualswitch $Network1

The final part of the process where you actually create the VM is simple. Create the directory location for the virtual hard drive (VHD). Then six cmdlets create and configure the new VM.

The New-VM command defines the machine and its location. New-VHD creates the VHD file and Add-VMDisk assigns it to the VM. Add-VMDrive adds a DVD drive to the machine, Set-VMMemory defines the amount of RAM and Add-VMNIC configures the network card.

Comments (0)

Skip to main content