PowerShell Create Hyper-V VM (Windows)

By | April 16, 2018

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum dui dui, sit amet maximus lectus facilisis quis. Aliquam ac quam scelerisque felis faucibus volutpat eget eu diam. Nullam erat odio, pretium et faucibus id, egestas eu mi. Integer dapibus vulputate lectus, in mollis arcu vehicula ac. Duis consectetur augue nec varius consectetur. Vivamus enim erat, faucibus in odio quis, cursus dapibus mi. Sed id ante id nisl tincidunt tincidunt id id leo. Ut finibus tellus velit, vel aliquam sapien maximus quis. Curabitur eget risus egestas quam pretium maximus sit amet eget sapien. Sed vel lectus vel enim interdum gravida. Quisque id porta diam.

$name = 'WinSrv'
$pathiso = 'D:\OS\en_windows_server_version_1709_updated_jan_2018_x64_dvd_100492040.iso'
$dataVHD = $name + "-DATA"


New-VM -Name $name -path C:\Hyper-V\ -MemoryStartupBytes 2GB -Generation 2 -NewVHDPath C:\Hyper-V\$name\$name.VHDX -NewVHDSizeBytes 127GB -SwitchName 'LAN Virtual Switch'
Set-VMMemory $name -DynamicMemoryEnabled $true -MinimumBytes 1GB
Get-VM $name | Set-VMProcessor -Count 4
New-VHD -Path C:\Hyper-V\$name\$dataVHD.VHDX -SizeBytes 254GB -Dynamic

#Enable-VMIntegrationService –Name  -VMName 
Enable-VMIntegrationService –Name 'Guest Service Interface' -VMName $name
Set-VMFirmware $name -EnableSecureBoot On -SecureBootTemplate MicrosoftWindows
#Set-VMFirmware $name -EnableSecureBoot On -SecureBootTemplate MicrosoftUEFICertificateAuthority
#Set-VMFirmware $name -EnableSecureBoot On -SecureBootTemplate OpenSourceShieldedVM

Add-VMScsiController -VMName $name
Add-VMHardDiskDrive -VMName $name -ControllerType SCSI -ControllerNumber 1 -ControllerLocation 0 -Path C:\Hyper-V\$name\$dataVHD.VHDX
Add-VMDvdDrive -VMName $name -ControllerNumber 1 -ControllerLocation 1 -path $pathiso
Set-VM -Name $name -AutomaticCheckpointsEnabled $false

#$network = Get-VMNetworkAdapter -VMName $name
#$vhd = Get-VMHardDiskDrive -VMName $name
$dvd = Get-VMDvdDrive -VMName $name
Set-VMFirmware -VMName $name -FirstBootDevice $dvd
#Set-VMFirmware -VMName $name -BootOrder $dvd, $network, $vhd

Start-VM -Name $name
Start-Process virtmgmt.msc
vmconnect localhost $name
#Get-VM -Name $name | remove-vm -Force
#Remove-Item -Path C:\Hyper-V\$name -Recurse -Verbose