Advertisement
guyrleech

Create Hyper-V VM from Template

Oct 4th, 2024 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Create new Hyper-V VM from a template (a SysPrep'd VM with no spanshots) by copying disk to new VM name , creating it and starting it
  2. ## Put the name of the virtual network switch in to connect to - in this example it is "External"
  3. ## Note that this line uses a V: drive for the VMs
  4.  
  5. $name=Read-Host "New VM name"; if( -Not ($old=Get-VM $name -EA 0) -and $name.Length -le 15 -and $name.length -gt 0 ) { md V:\VMs\$name && cp 'V:\VMs\Virtual Hard Disks\Windows 11 Template 24H2.vhdx' "V:\VMs\$name\$name.vhdx" -Force:$false && ($vm=New-VM -Name $name -SwitchName "External" -VHDPath "V:\vms\$name\$name.vhdx" -Generation 2 -MemoryStartupBytes (2GB) ) && Set-VMProcessor -Count 2 -VM $vm && Set-VMMemory -MaximumBytes (4GB) -DynamicMemoryEnabled $true -VM $vm && Add-VMDvdDrive -VM $vm && Start-VM -VM $vm } else { Write-Error "$(if(! $old) { "Name $name is too long at $($name.length)" } else { "$name already exists" })" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement