You can create a new VM by using one of the following two methods:

  • Using Windows GUI.
  • Using PowerShell commands.

This article will discuss how to create a Hyper-V virtual machine using PowerShell commands.

Create a Hyper-V Virtual Machine using PowerShell

Step 1. Open PowerShell command with elevated privileges.

Step 2. Execute the following command to create a new VM:

New-VM -Name Testmachine -path C:\vm-machine --MemoryStartupBytes 512MB

Step 3. Execute the following command to create a new virtual hard disk:

New-VHD -Path c:\vm-Machine\Testmahcine\Testmachine.vhdx -SizeBytes 10GB -Dynamic

Step 4. Execute the following command to attach our new virtual hard disk to the VM:

Add-VMHardDiskDrive -VMName TestMachine -path "C:\vm-machine\Testmachine\Testmachine.vhdx"

Step 5. Execute the following command to map an ISO image to VM CD/DVD (which will be used to install the OS inside the VM, so this can be a Windows or Linux DVD image):

Set-VMDvdDrive -VMName -ControllerNumber 1 -Path

Step 6. Start the new VM:

Start-VM -Name
How to Create a Hyper-V Virtual Machine using PowerShell
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

3 thoughts on “How to Create a Hyper-V Virtual Machine using PowerShell

  • Avatar
    December 30, 2018 at 7:20 am
    Permalink

    Hi,
    I am trying to add an existing newly created nano server into Hyper-V using only Powershell.
    I am very new to powershell and could not find an example. I have packages etc installed in Nano so all I need to add it to Hyper-v, give some ram and networking.
    Can you give such an example?

    Reply
  • Avatar
    April 29, 2020 at 11:16 am
    Permalink

    Hey i tried this below
    New-VM -Name Testmachine -path C:\vm-machine –MemoryStartupBytes 512MB

    it showed the below error

    New-VM : Cannot bind parameter ‘Generation’. Cannot convert value “536870912” to type “System.Int16”. Error: “Value
    was either too large or too small for an Int16.”
    At line:1 char:58
    + New-VM -Name Test -path C:\vm-test1 –MemoryStartupBytes 512MB
    + ~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-VM], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.HyperV.PowerShell.Commands.NewVM

    but when i removed a “-” for MemoryStartupBytes, It worked

    so what is the difference?
    and why this command didn’t work for me(New-VM -Name Testmachine -path C:\vm-machine –MemoryStartupBytes 512MB) ?

    but this worked
    New-VM -Name Testmachine -path C:\vm-machine -MemoryStartupBytes 512MB

    Reply
  • Avatar
    May 29, 2020 at 2:03 pm
    Permalink

    how do i use an existing hard disk instead of creating an new hard disk?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*