Script to build a VM in Hyper-V and boot from an ISO

This week I was doing a ConfigMgr 2012 R2 class in New York, and one of students asked if he could get the script I used to create the VM I use to build my reference images. Sure, here it is:

$VMLocation = "C:\VMs"
$VMISO = "C:\ISO\MDT Build Lab x86.iso"
$VMNetwork = "Internal"

# Create REF001
$VMName = "REF001"
$VMMemory = 4096MB
$VMDiskSize = 60GB
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-VMProcessor -VMName $VMName -Count 2
Set-VMDvdDrive -VMName $VMName -Path $VMISO -Verbose
Start-VM -VMName $VMName

$VM = Get-VM -Name $VMName
while ($VM.State -ne "off")
    write-host "The VM is still running" 
    sleep 20 

Remove-VM -Name $VMName -Force
Remove-Item -Recurse -Force $VMLocation\$VMName

Happy Deployment,
/ Johan

About the author

Johan Arwidmark

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments