Sep 08 2021 02:31 PM
Assuming we want to create multiple Hyper-V VMs via PowerShell,
Pre-requisites
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........."