Introduction

This blog post will be covering how to create a Hyper-V VM using Powershell.

Create a Hyper-V VM using Powershell

Start by opening an elevated Powershell window.

Powershell in administrative mode

Enter the command below, replacing the settings for your situation.

New-VM -Name DC -MemoryStartupBytes 512MB -Path D:\ws2012.local

The above command creates a Virtual Machine called DC in D:\ws2012.local with 512 MB memory. Note that a subfolder is created with the name of the Virtual Machine. So in my case, the Virtual Machine will be placed in D:\ws2012.localDC.

Create a VM using the Powershell command New-Vm

Now we will create a new dynamically expanding Virtual Hard Drive with the new *.vhdx file extension and with a maximum size of 60 GB using:

New-VHD -Path D:ws2012.localDCDC.vhdx -SizeBytes 60GB -Dynamic
New Virtual Hard Drive VHDX Powershell Windows Server 2012

The next step is to connect the newly created Virtual Hard Drive with the Virtual Machine.

Add-VMHardDiskDrive -VMName DC -Path D:ws2012.localDCDC.vhdx" 

Now you might want to connect an *.iso file for the Windows installation:

Set-VMDvdDrive -VMName DC -ControllerNumber 1 -Path "<path to ISO>
Connect DVD Iso Virtual Machine Powershell Windows Server 2012 Hyper-V

Start the VM:

Start-VM –Name DC

View the status of the VM:

Get-VM DC
Start Virtual Machine Powershell Windows Server 2012 Hyper-V

I prefer to also set up Dynamic Memory for my machine. In order to do this you first need top the machine using:

Stop-VM DC

Then run:

Set-VMMemory -VMName DC -DynamicMemoryEnabled $true -StartupBytes 512MB -MinimumByte 512

Summary

There are of course a lot more settings you can configure, but this will get you up and running. Once you get the hang of Powershell, you won’t be going back to create your machines in the GUI, as this is much faster 🙂

References

Related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here