Create Multiple Hyper-V Virtual Machines via PowerShell

Windows Server 2012 Logo

For a demo installations I needed a lot of Virtual Machines so I had to choose between creating all the Virtual Machines via Hyper-V Manager which is not really a great option. The best option in this case would be doing this via Windows PowerShell, and with the new Hyper-V PowerShell module in Windows Server 2012 this is very easy and took me only some minutes to get this quick and dirty script. Because this script is only used for my lab or demo environments there is no error handling included.

 
1..9 | % {
 
$VMName = "WS2012-VM0$_"
 
Write-Host "Create VM $VMName" -BackgroundColor Black -ForegroundColor White
New-VM -Name $VMName -MemoryStartupBytes 2GB -SwitchName VMNET -Path <a href="file://\\sofs01\Share01\&quot; data-mce-href=">\\sofs01\Share01\</a>
 
Write-Host "Copy VHD..." -BackgroundColor Black -ForegroundColor Green
Copy-item '\\sofs01\Share01\ISOs\Disk\Virtual Hard Disks' -Destination "\\sofs01\Share01\$VMName" -Recurse
 
Write-Host "Attach VHD..." -BackgroundColor Black -ForegroundColor Green
Add-VMHardDiskDrive -VMName $VMName -ControllerType IDE -ControllerNumber 0 -Path <a href="file://\\sofs01\Share01\$VMName\Virtual Hard Disks\WS2012.vhdx">\\sofs01\Share01\$VMName\Virtual Hard Disks\WS2012.vhdx</a>
 
Write-Host "Configure Virtual Machine..." -BackgroundColor Black -ForegroundColor Green
Set-VM -Name $VMName -DynamicMemory -MemoryMaximumBytes 4GB -MemoryMinimumBytes 512MB -MemoryStartupBytes 1GB -ProcessorCount 2
 
Write-Host "Startup Virtual Machine: $VMName..." -BackgroundColor Black -ForegroundColor Green
Start-VM -Name $VMName
 
}