Introduction

As with most of Microsoft’s products nowadays, you can either use the GUI or use Powershell.

When you wish to create a new virtual machine in Hyper-V, you can either use the virtual machine wizard in the Hyper-V Manager or create the Hyper-V virtual machines using 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 VM using the Powershell command New-Vm

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
New Virtual Hard Drive VHDX Powershell Windows Server 2012

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>
Connect DVD Iso Virtual Machine Powershell Windows Server 2012 Hyper-V

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:

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

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:

Stop-VM DC

Then run:

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

Conclusion

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!

Suggested Resources

9 COMMENTS

  1. After you build the virtual machine using Powershell inside the Hyper-V server 2012. (Let say I instill Windows Server 2012 or Windows 8 onto that virtual machine.) Can I build & manager any & all other virtual machines form the GUI on that virtual machine (Windows Server 2012 or Windows 8)? Thank you for your help and time. Have a nice day.

  2. Hi Daniel,

    thank you for the script.

    I also tried to disable checkpoints via PowerShell because I use Hyper-V with Windows 10 Pro and the following command just didn’t work:
    Set-VM -Name $Name -AutomaticCheckpointsEnabled $False

    Set-VM doesn’t support the parameter AutomaticCheckpointsEnabled right now. I can’t find the source but I read it will change with Windows Update 1907 (yet to be released).

    Hope this helps someone finding this resource.

    Anyway thank you
    Andreas

  3. Can’t remember which blog post took me over here from the beginning, but never mind. Thanks for that one.

    One PS script being used frequently by me is one that deploys a few VM’s for test and lab. For that task I use Parent/Child configuration with differencing disks. Maybe that’s another blog post for the future…? 😉

    Ooh, almost forgot. It’s a small world. We’ve almost been neighhours back in the days, wen’t to school with your little sis’.

    Cheers from Kungsbacka

LEAVE A REPLY

Please enter your comment!
Please enter your name here