How to Create Multiple Hyper-V VMs via PowerShell


Assuming we want to create multiple Hyper-V VMs via PowerShell,


  • Below script thinks that you have VMName list and Creates VM names according to each name which are separated coma and single quoted VM Names in a variable.




  • Add an ISO file for OS Installation and puts the HDD in to the directory you will mention in $VMLOC variable and assigns existing Hyper-V Switch to all VM’s.


Script starts---------

#Below command will load the Hyper-V module for PowerShell.
Get-Command -Module Hyper-V

# This script creates a Multiple VM's Based on the Names you provided.

#Enter the VM names as mentioned below.
$VMName = 'Server001','server002'

#Enter the ISO File path which contains the Windows Installation files
$ISOpath = "D:\library\Windows Server 2008 R2 SP1_x64fre_server_eval_en-us-DVD.iso"

#Path of the VM HDD file stored
$VMLOC = "d:\test"

#Name of virtual switch which will be used in the VMs
$VMNet = "vEthernet-ADDC-M2"

#Create the VM's
Foreach($vm in $VMName) { New-VM -Name $VM -Generation 2 -SwitchName $VMNet
New-VHD -Path "$VMLOC\$VM\$vm.vhdx" -Dynamic -SizeBytes 40GB
ADD-VMHardDiskDrive -VMName $vm -Path "$VMLOC\$VM\$vm.vhdx"
Set-VM $VM -MemoryStartupBytes 1GB
Add-VMDvdDrive -VMName $vm -Path $ISOpath
Set-VMFirmware -VMName $vm -FirstBootDevice ((Get-VMFirmware -VMName $vm).BootOrder |
Where-Object Device -like *DvD*).Device

#Starts all of the VMs and installation of OS will be started.
Start-VM -Name $VMName

Script Ends-------------


The only problem I have faced with this script is by using Microsoft ISO files for OS, which is marked by an end user's input when installation starts, asking for "Press any key to start installation........."

1 Reply
Nice, I used something similar in the past.. Perhaps you could use a tempate vhdx with a sysprepped VM as an option?