Install the Hyper-V role and create virtual machines using PowerShell

This blog post covers how to install Hyper-V on a windows machine using PowerShell and create virtual machines. To learn more about Hyper-V including system requirements, see Hyper-V overview. Also make sure that you have administrative rights on the machine for doing so.

Install Hyper-V Role

On a windows server:

Open Windows PowerShell session with elevated user rights and run the following command:

Install-WindowsFeature -Name Hyper-V -ComputerName  
-IncludeManagementTools -Restart

Do not use -ComputerName parameter if you are installing hyper-v on the local machine itself. It will restart server after installing hyper-v. Once it comes up, logon with administrative user. Again open a PowerShell session with elevated user rights and run below command:

Get-WindowsFeature | where {$_.installstate -eq "installed"}

You should be able to see a Hyper-V in the list of features.

On a windows client:

Open Windows PowerShell session with elevated user rights and run the following command:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

After this, reboot machine using

Restart-Computer -force

Create Virtual Switch

A virtual switch allows hyper-v virtual machines to communicate with other computers. You can choose to create 3 types of virtual switches:

Connection type Description
External Gives virtual machines access to a physical network to communicate with servers and clients on an external network. Allows virtual machines on the same Hyper-V server to communicate with each other.
Internal Allows communication between virtual machines on the same Hyper-V server, and between the virtual machines and the management host operating system.
Private Only allows communication between virtual machines on the same Hyper-V server. A private network is isolated from all external network traffic on the Hyper-V server. This type of network is useful when you must create an isolated networking environment, like an isolated test domain.

First we need to find the network adapter in the physical host using below command:


Then make a note of the network adapter name from above command. After this, we can use New-VMSwitch cmdlet to create an external virtual switch:

New-VMSwitch -name  -NetAdapterName  
-AllowManagementOS $true

To create an internal switch, we can use below command:

New-VMSwitch -name InternalSwitch -SwitchType Internal

and for private switch:

New-VMSwitch -name PrivateSwitch -SwitchType Private

Create Virtual Machine

We can create new virtual machine using New-VM cmdlet. For starters, we can use below commands to create a virtual machine where we need to install an OS from an iso image:

New-VM -Name "dc01" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName "ExternalSwitch" -NewVHDPath "E:
\VMs\VHDs\dc01.vhdx" -NewVHDSizeBytes 100GB -Generation 1

Here, -Name indicates name of the virtual machine,

-BootDevice indicates preferred boot device for the virtual machine (Allowed values are CD, Floppy, LegacyNetworkAdapter, IDE, NetworkAdapter, and VHD),

-SwitchName indicates name of the virtual switch,

-MemoryStartupBytes indicates starting memory for virtual machine(The default value is 512 MB),

-Generation indicates type of the virtual machine (The values that are valid are 1 and 2)

-NewVHDPath creates a new virtual hard disk with the specified path and connects it to the new virtual machine and

-NewVHDSizeBytes specifies the size of the dynamic virtual hard disk that is created and attached to the new virtual machine

We can also choose to create virtual machine using an existing vhd file:

New-VM -Name "dc01" -MemoryStartupBytes 1GB -VHDPath d:\vhd\BaseImage.vhdx

This will create VMs but they will not start it.

Attach an ISO file to virtual machine

To connect an iso image to the virtual machine, use below command:

Set-VMDvdDrive -VMName  -Path

Start Virtual Machine

To see virtual machines in a hyper-v hosts, use command Get-VM:

PS C:\Windows\system32> Get-VM

Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
dc01 Off 0 0 00:00:00 Operating normally

To start a virtual machine, use command:

Start-VM  -Name

All of these cmdlets supports -ComputerName parameter which makes it way easier to manage virtual machine on other hyper-v hosts as well.

Connect to Virtual Machine

for this, we can use the vmconnect.exe utility which comes with hyper-v itself. It is command line equivalent of vm console. We can check syntax for this using below command:

vmconnect.exe syntax
vmconnect.exe syntax

To connect to virtual machine on local server, we can use below command on PowerShell prompt:

.\vmconnect.exe localhost

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s