In this article, I cover how to create Hyper-V virtual machine with PowerShell, the Hyper-V cmdlets, and the Hyper-V module. To run the Hyper-V cmdlets in PowerShell referred to in this article, you first need to Enable the Hyper-V Role.
You can create Hyper-V virtual machines using the Hyper-V Manager or PowerShell. In the following steps, we will use a few commands and then create a virtual machine via PowerShell. One of the usual IT administrator’s activities is creating a new virtual machine. We can create a new virtual machine using the PowerShell command or Hyper-V Manager.
Table of Contents
I will create a new virtual machine with the below resources. Windows Server 2012, 2 GB of RAM, and 60 GB of disk space.
Create Hyper-V Virtual Machine with PowerShell
Right-click on the Windows Server start button and then open Windows PowerShell with admin rights.
Type the following command to create a new virtual machine with a startup RAM of 2 GB. Press enter
New-VM -Name Server2012 -MemoryStartupBytes 2GB -Path E:\Hyper-V
Note that a new folder is created with the name of the virtual machine (Server2012). So, in this case, Hyper-V will place the virtual machine in E:\Hyper-V.
Create a Dynamically Expanding Disk
In this step, we will create a new dynamically expanding virtual disk with the extension *.vhdx and a maximum size of 60 GB.
Enter the below command, and then press enter.
New-VHD -Path E:\Hyper-V\Server2012.vhdx -SizeBytes 60GB –Dynamic
Connect the virtual hard drive to the VM
In this step, we will connect the newly created virtual drive with the virtual machine.
Enter the below command, and then press enter.
Add-VMHardDiskDrive -VMName Server2012 -Path E:\Hyper-V\Server2012.vhdx
Connect an ISO file to the Windows installation
Type the following command and then hit enter.
Set-VMDvdDrive -VMName Server2012 -ControllerNumber 1 -Path ‘E:\Servers\Windows Server 2012.iso’
Start the virtual machine
Start-VM –Name Server2012
View the status of the virtual machine:
Get-VM Server2012
Conclusion
Of course, there are many other settings you can configure, but this will get you up and running with the virtual machine. Once you get familiar with PowerShell, you will not be going back to creating your virtual machines in the GUI, because this is much faster.
How do you create a Hyper-V virtual machine with PowerShell? Please leave a comment in the comment box.
Refer to Microsoft to learn in detail how to create a Hyper-V virtual machine.