New-VM Template: Single VHDX File

The following PowerShell is what we use to create a new VM that will have only one VHDX file associated with it for the OS and possibly an LoB.

# Generic Template 2020-03-13

# New-VM $VMName
$VMName = "MyVM"
$VMMemory = 4GB
$vCPUCount = 2
$VMDiskSize0 = 75GB
$vSwitch = "Shared vSwitch"
$DriveLetter = "L:"
$ISO = "$($DriveLetter)\ISOs\WinServ19B-2020-03-13.ISO"
$AutoMaticStartDelay = "15"
$VLANID = 11


# Create the VM
New-VHD -Path "$($DriveLetter)\Hyper-V\Virtual Hard Disks\$($VMName)_D0.VHDX" -Dynamic -SizeBytes $VMDiskSize0
New-VM -Name $VMName -Generation 2 -Memory $VMMemory –SwitchName "$vSwitch"
Set-VMProcessor $VMName -Count $vCPUCount
Add-VMHardDiskDrive -VMName $VMName -Path "$($DriveLetter)\Hyper-V\Virtual Hard Disks\$($VMName)_D0.VHDX"
Add-VMDvdDrive -VMName $VMName -ControllerNumber 0 -ControllerLocation 63
Set-VMDvdDrive -VMName $VMName –Path $ISO
Set-VM –Name $VMName –AutomaticStartAction Start -AutomaticStartDelay $AutoMaticStartDelay -AutomaticStopAction ShutDown
Add-ClusterVirtualMachineRole -VirtualMachine $VMName

# Set the VM's VLAN ID
Set-VMNetworkAdapterVlan –VMName $VMName –Access –VlanId $VLANID

# Remove .ISOs from all VMs
Set-VMDvdDrive -VMName * –Path ""