How To Create and Configure a Hyper-V VM Using Powershell
This blog post will be covering how to create a Hyper-V VM using Powershell.
From the CMD, type Powershell to get in to the Powershell prompt.
New-VM -Name DC -MemoryStartupBytes 512MB -Path D:\ws2012.local
This will create a Virtual Machine called DC in D:\ws2012.local with 512 MB memory. Note that a subfolder will be created with the name of the Virtual Machine. So in my case, the Virtual Machine will be placed in D:\ws2012.local\DC .
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.local\DC\DC.vhdx -SizeBytes 60GB -Dynamic
The next step is to connect the newly created Virtual Hard Drive with the Virtual Machine.
Add-VMHardDiskDrive -VMName DC -Path D:\ws2012.local\DC\DC.vhdx"
Now you might want to connect an *.iso file for the Windows installation:
Set-VMDvdDrive -VMName DC -ControllerNumber 1 -Path "<path to ISO>
Start the VM:
Start-VM –Name DC
View the status of the VM:
I prefer to also set up Dynamic Memory for my machine. In order to do this you first need top the machine using:
Set-VMMemory -VMName DC -DynamicMemoryEnabled $true -StartupBytes 512MB -MinimumByte 512
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 🙂
About the author
Daniel Classon works as a Senior Consultant at Mansoft, focusing on Microsoft Configuration Manager, Windows 10 and Powershell