Share on Facebook0Share on Google+0Share on LinkedIn0Share on Reddit0Tweet about this on Twitter0

For large Hyper-V infrastructure, IT people use often Virtual Machine Manager (VMM) to automate tasks as VM deployment. VMM provides profiles (hardware, operating system and application) and then we use these profiles to build a VM Template. Most of the time, a VM Template contains a VM hardware configuration and the OS configuration (join the domain, product key and so on). Then VMs are deployed from templates which decrease the time to deploy a working VM.

The convenient about VMM is its price. VMM is expensive and it is often medium/large business which implement VMM. So for other companies which can’t buy VMM, are they forced to do without VM deployment automation? Of course no. Other tools can help us to make automation without VMM and it is called PowerShell.

Since Windows Server 2012, a lot of Hyper-V PowerShell commands (in Hyper-V module) have been added. Now easily we can create, configure and manage VM from PowerShell.

In this topic we will see how to create and set a Virtual Machine from PowerShell. The scripts presented are examples. They can be changed, modified and improved regarding your needs. They are here for explanation.

To follow this topic, I assume that you have knowledge about Hyper-V and PowerShell.

Create a VM

To create a Virtual Machine from PowerShell, you can run the New-VM PowerShell cmdlet as below:

  • Name: Name of the Virtual Machine
  • Path: Path where will be stored VM files
  • NewVHDPath: Create a VHD(X) file to the specified path (Dynamic disk)
  • New-VHDSizeBytes: Size of the VHD(X) file
  • Generation: VM generation (1 or 2)
  • MemoryStartupBytes: Memory assigned to the VM (static Memory)
  • SwitchName: switch name where the network adapter will be connected

Below you can find an example of VM creation with the above command:


If you don’t want to attach now a VHD(x) with your VM because you have a syspreped VHDX, you can run the below command:

Configure a VM

To configure deeply a VM, you can use the Set-VM PowerShell cmdlet. Below an example:

  • Name: Name of the VM you would like to edit
  • ProcessorCount: number of vCPU that you want to assign to the VM
  • DynamicMemory: Enable the Dynamic Memory
  • MemoryMinimumBytes: set the minimum memory value
  • MemoryStartupBytes: set the startup memory value
  • MemoryMaximumBytes: set the maximum memory value
  • AutomaticStartAction: action which is run when the Hyper-V service is starting (Nothing, Start, StartIfRunning)
  • AutomaticStartDelay: number of second to wait before the automatic start action is run
  • AutomaticStopAction: action which is run when the Hyper-V service is stopping (Save, Shutdown, TurnOff)

Below you can find an example of VM configuration with the above command:



If you want to configure the VM with static memory you can use the below command:

Manage VM Storage

Add the OS disk and set the primary boot

If you have run the below command to create a VM without VHD(X), maybe you would like to attach now your syspreped VHD(X):

To attach the VHD(x) to the VM, you have just to run Add-VMHardDiskDrive PowerShell command:

  • VMName: Name of the related VM
  • Path: Path to the VHD(X) file

Below you can find an example of attaching a VHD(X) file to a VM:



Now you can set this disk to primary boot by using the Set-VMFirmware PowerShell cmdlet:

  • VMName: VM of the related VM

Below you can find an example of the using of the above command:



StarWind HyperConverged Appliance is a turnkey, entirely software-defined hyperconverged platform purpose-built for intensive virtualization workloads. Bringing the desired performance and reducing downtime, the solution can be deployed by organizations with limited budgets and IT team resources. Also, it requires only one onsite node to deliver HA for your applications that make the solution even more cost-efficient.
Find out more about ➡ StarWind HyperConverged Appliance

Adding additional VHD(X)

To create a new VHD(X) you can use the New-VHD cmdlet as below:

  • Path: absolute path to the VHD(X) file
  • SizeBytes: Size of the VHD(X)
  • Dynamic (can be replace by fixed): Dynamic or fixed VHD(X)

Once you have created the disk, you can attach it by using Add-VMHardDiskDrive as below:

Manage network adapters

Add network adapters

To add a network adapter, you can run the below command:

  • VMName: name of the VM
  • SwitchName: Name of the VM Switch to connect to
  • Name: Name of the network adapter

Below you can find an example of the above command:



Configure network adapters

There are a lot of options in network adapters configuration. You can configure all of them by using PowerShell but I present only some of them. You can find the help of these PowerShell cmdlet in TechNet. To configure the Network Adapter, you can run the below command:

  • MacAddressSpoofing: Enable or disable Mac Spoofing
  • DHCPGuard: Enable or disable DHCP Guard
  • RouterGuard: Enable or disable router guard

Below you can find an example of the use of this PowerShell cmdlet:



Set the VLAN

To set the VLAN on a network adapter, you can use the Set-VMNetworkAdapterVLAN PowerShell cmdlet:

  • Access: Set the Access mode on the network adapter
  • VlanId: set the VLAN ID value

To leave untagged the network adapter, you can run the below command:

Below you can find an example of this PowerShell command:



Script to automatically deploy a VM

Below you can find a script that I have written to deploy several VMs from the same configuration. This script is not perfect and can be improved by adding try/catch. You can also modify parameters to be passed in command line as arguments. In this way you will be able to make a loop to deploy quickly several VMs. To use this script you need a sysprep VHD(X).


If you can’t buy VMM and you want to deploy automatically VM, it is still possible by using PowerShell. All examples presented are not exhaustive. There are several other combinations possible. All cmdlet documentation can be found in the TechNet.

Views All Time
Views Today
Appreciate how useful this article was to you?
1 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 5
5 out of 5, based on 1 review
Back to blog
The following two tabs change content below.
Romain Serre
Romain Serre
Senior consultant at Exakis
Romain Serre works in Lyon as a Senior Consultant. He is focused on Microsoft Technology, especially on Hyper-V, System Center, Storage, networking and Cloud OS technology as Microsoft Azure or Azure Stack. He is a MVP and he is certified Microsoft Certified Solution Expert (MCSE Server Infrastructure & Private Cloud), on Hyper-V and on Microsoft Azure (Implementing a Microsoft Azure Solution).