How to Create a Hyper-V Virtual Machine using PowerShell

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

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

  1. Avatar

    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?

  2. Avatar

    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

  3. Avatar

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


Leave a Comment