PowerShell Guide: New VM PowerShell

The following are some sample PowerShell steps to create VMs on Hyper-V.

We copy and paste the code into a new NotePad window. Then, CTRL+H to do a Find & Replace for the VM’s name and/or destination.

Standalone Hyper-V Host

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# New-VM VMNameHere
New-VHD -Path "X:\Hyper-V\Virtual Hard Disks\VMNameHere_D0-75GB.VHDX" -Dynamic -SizeBytes 75GB
New-VM -Name VMNameHere -Generation 2 -Memory 4GB –SwitchName "vSwitch"
Set-VMProcessor VMNameHere -Count 2
Add-VMHardDiskDrive -VMName VMNameHere -Path "X:\Hyper-V\Virtual Hard Disks\VMNameHere_D0-75GB.VHDX"
Add-VMDvdDrive -VMName VMNameHere -ControllerNumber 0 -ControllerLocation 63
Set-VMDvdDrive -VMName VMNameHere –Path "X:\ISOs\WinServ16B-2018-07-13.ISO"
Set-VM –Name VMNameHere –AutomaticStartAction Start -AutomaticStartDelay 30 -AutomaticStopAction ShutDown

# Assign a VLAN
Set-VMNetworkAdapterVlan –VMName VMNameHere –Access –VlanId 50

# Make the newly created VM HA
Add-ClusterVirtualMachineRole -VirtualMachine VMNameHere