, , ,

Create Multiple Hyper-V Virtual Machines via PowerShell

1–2 minutes
3 comments on 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 \\sofs01\Share01\

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 \\sofs01\Share01\$VMName\Virtual Hard Disks\WS2012.vhdx

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

}

3 responses

  1. Dino Avatar

    Examples include attaching physical disks to a virtual machine and modifying processor settings.

  2. dm Avatar

    can you please tell me what is the meaning of “1..9 | % {“

    1. Thomas Maurer Avatar

      Count from 1 to 9 :)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.