Speed Lab – Bulk create Hyper-V Virtual Machines with Windows PowerShell

Hyper-V gives you an environment to quickly and efficiently run test configurations, which has made it a popular lab environment. The only catch is, you need to create and configure virtual machines (VMs) on a regular basis.
Whether you need to test new or standardized server setups, creating virtual machines is still a boring.

If you’re looking for an easy way to build Hyper-V images, Windows PowerShell scripts are a viable solution. The extent to which you can customize Windows PowerShell and its flexible command structure makes it suitable for any environment. When combined with an automated or unattended setup strategy, virtually any computer configuration is possible. Underneath the examples for Generation 1 and Generation 2 machines.

Prequisits;
-Path E:\VMs exist
-The ISO file and location is in place
-The Virtual network “Internal” is implemented in Hyper-V

*************Bulk create generation 2 virtual machines**************

$VMLocation = "E:\VMs"
$VMISO = "E:\ISO\W2012R2.iso"
$VMNetwork = "Internal"

# Create DC01
$VMName = "DC01"
$VMMemory = 1024MB
$VMDiskSize = 40GB
New-VM -Name $VMName -Generation 2 -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose
Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose
Set-VMDvdDrive -VMName $VMName -Path $VMISO -Verbose

# Create CM01
$VMName = "CM01"
$VMMemory = 6144MB
$VMDiskSize = 300GB
New-VM -Name $VMName -Generation 2 -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose
Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose
Set-VMDvdDrive -VMName $VMName -Path $VMISO -Verbose

*************Bulk create generation 1 virtual machines**************

$VMLocation = "E:\VMs"
$VMISO = "E:\ISO\W2012R2.iso"
$VMNetwork = "Internal"

# Create DC01
$VMName = "DC01"
$VMMemory = 1024MB
$VMDiskSize = 40GB
New-VM -Name $VMName -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose
Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose
Set-VMDvdDrive -VMName $VMName -Path $VMISO -Verbose

# Create CM01
$VMName = "CM01"
$VMMemory = 6144MB
$VMDiskSize = 300GB
New-VM -Name $VMName -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose
Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose
Set-VMDvdDrive -VMName $VMName -Path $VMISO -Verbose