As with most of Microsoft’s products nowadays, you can either use the GUI or use Powershell.
Using Powershell allows you to create Hyper-V virtual machines using templates systematically, and you can also automate the whole process.
In this blog post, I cover how to create Hyper-V virtual machines using Powershell, the Hyper-V module, and Hyper-V CMDlets,
How to use the Hyper-V VM Powershell CMDLets
To run the Hyper-V CMDLets in Powershell mentioned in this blog post, you first need to install the Hyper-V role.
The easiest way to do this is to open Powershell and run the following command:
Install-WindowsFeature -Name Hyper-V -ComputerName <computer_name> -IncludeManagementTools -Restart
How to create a VM using Powershell in Hyper-V
Start by opening an elevated Powershell window.
Enter the command below to create a new virtual machine with a startup RAM of 512 GB
New-VM -Name DC -MemoryStartupBytes 512MB -Path D:\ws2012.local
Note that a subfolder is created with the name of the virtual machine. So in my case, Hyper-V will place the VM in D:\ws2012.localDC.
Create a dynamically expanding disk
The next step is to create a new dynamically expanding Virtual Hard Drive with the *.vhdx file extension and with a maximum size of 60 GB using:
New-VHD -Path D:\ws2012.localDCDC.vhdx -SizeBytes 60GB -Dynamic
Connect a virtual hard drive with the VM
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"
Connect an ISO file for the Windows installation
Connect a *.iso file for the Windows installation:
Set-VMDvdDrive -VMName DC -ControllerNumber 1 -Path "<path to ISO>
Start the virtual machine
Start the virtual machine:
Start-VM –Name DC
View the status of the virtual machine
View the status of the virtual machine:
Configure dynamic memory
I also prefer to set up dynamic memory for my machine. This means that the virtual machine will only use the memory required.
To accomplish this, you first need to stop the machine using:
Set-VMMemory -VMName DC -DynamicMemoryEnabled $true -StartupBytes 512MB -MinimumByte 512
There are, of course, many 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 and less error-prone 🙂
How do you create virtual machines in Hyper-V? Please leave a comment below!