PowerShell’s Docs PowerShell Hyper-V Management Create a Hyper-V virtual switch with PowerShell

In this second post in my series about Hyper-V management with PowerShell I will explain how to create a Hyper-V virtual switch with PowerShell. In my previous post I explained how to install Hyper-V with PowerShell.

Virtual switch types

The type of virtual switch you'll create totally depends upon how you want your virtual machines to communicate over the network. Hyper-V allows you to create three types of virtual switches:

  1. Private—The VMs connected to a private virtual switch can communicate with each other on the same Hyper-V server but not with the external network (such as the Internet or Intranet). The VMs cannot even communicate with the host operating system running the Hyper-V hypervisor.
  2. Internal—The VMs connected to an internal virtual switch can communicate with each other and with the host operating system. Since this virtual switch is not bound to the physical network interface of the host system, the VMs connected to an internal virtual switch cannot access external network resources. However, you could set up a "NAT network" to get Internet access on the VMs.
  3. External—The VMs connected to an external virtual switch can fully communicate with each other, with the host, with the external network, and with the Internet. This virtual switch is bound to the physical network interface of the host system. The physical network interface can be either a single NIC or a team interface (multiple NICs acting as a team).

Create a virtual switch

To create an external virtual switch, we need to specify a physical network adapter with which to bind so that it can connect to a network. You can use the following command to get a list of all the physical network adapters available on your Hyper-V host:

Get-NetAdapter | ft ifIndex, Name, Status -Auto
List network adapters using PowerShell

List network adapters using PowerShell

We will use the Ethernet interface to bind with the external virtual switch.

To create the new external virtual switch, use the following command:

New-VMSwitch -Name "vEthernet" -NetAdapterName Ethernet -AllowManagementOS:$true
Create an external virtual switch using PowerShell

Create an external virtual switch using PowerShell

This command creates a new virtual switch with the name vEthernet. The -NetAdapterName parameter is used to specify the physical network adapter to bind. The -AllowManagementOS parameter with a $true value is used to allow the host OS to share the virtual switch and physical NIC with the VMs.

If you want to create an internal or private virtual switch, you can use the -SwitchType parameter with a value of either internal or private, as shown in the following commands:

New-VMSwitch -Name "InternalSwitch" -SwitchType Internal

This command creates an internal virtual switch with the name InternalSwitch.

New-VMSwitch -Name "PrivateSwitch" -SwitchType Private

This command creates a private virtual switch with the name PrivateSwitch.

View virtual switches

You can use the following command to list one or more virtual switches on the host:

Subscribe to 4sysops newsletter!

Get-VMSwitch
List virtual switches using PowerShell

List virtual switches using PowerShell

This command lists all the virtual switches created on the Hyper-V host server. On my server, I have created only one external virtual switch with the name vEthernet. We will connect the VMs that we will create in the next section to this virtual switch.

Articles in seriesManage Hyper-V with PowerShell

Discussion (0)

There are no comments for this doc yet.

Leave a Reply

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

Articles in seriesManage Hyper-V with PowerShell
© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account