PowerShell Script – Create A Hyper-V Virtual Machine Using PowerShell

In this article, I’ll show how I create a Microsoft Hyper-V Virtual Machine running Windows Server 2016 on a Windows Server 2016 Hyper-V Host using a PowerShell script.

Microsoft Hyper-V Server 2016 Is currently on Its 8th version release and shows great maturity and advanced feature set In terms of performance and options like GEN 2 VMs, PowerShell Direct, Dynamic Memory etc.

One of the best features I like In Hyper-V 2016 Is the ability to run Hyper-V Host In Windows Nano Server 2016.

In the below, Code I’ll create a new Virtual Machine, With 750MB RAM, GEN 2 VM and 40GB HDD.

Note: Please change the Names, ISO path, etc to match your environment: 

new-vm -Name NT05 -MemoryStartupBytes 750mb -Generation 2 -NewVHDPath C:\VMs\NT05.vhdx -NewVHDSizeBytes 40gb -BootDevice vhd

Add-VMDvdDrive NT05

Start-Sleep -Seconds 5

Set-VMDvdDrive -VMName NT05 -Path "C:\install\SW_DVD9_Win_Svr_STD_Core_and_DataCtr_Core_2016_64Bit_English_-2_MLF_X21-22843.ISO" -ToControllerNumber 0 -ToControllerLocation 1

Start-Sleep -Seconds 5

$dvd = Get-VMDvdDrive -VMName NT05

Set-VMFirmware -VMName NT05 -FirstBootDevice $dvd

Start-Sleep -Seconds 5

start-vm nt05

Once the script Is done, The VM will be created and start up.

Now, I can easily use PowerShell Direct to connect to or any other method