How to Create a Virtual Switch in Hyper-V Using PowerShell [New-VMSwitch]

Whether you're new to Hyper-V or seeking to enhance your automation skills, this article covers Hyper-V virtual switches you need to know.

Crystal

By Crystal / Updated on January 16, 2025

Share this: instagram reddit

What are virtual switch types in Hyper-V

A virtual switch in Hyper-V acts as a bridge, enabling virtual machines to connect with each other, the Hyper-V host operating system, or external networks. These switches simulate physical network switches but are entirely software-based. Hyper-V provides three types of virtual switches:

  • External Switch: Connects virtual machines to external physical networks.
  • Internal Switch: Allows communication between the host and virtual machines but not with external networks.
  • Private Switch: Limits communication strictly between virtual machines on the host.

Each type serves a specific purpose, and the choice depends on the desired networking configuration.

Next, we will walk through the detailed steps to create a virtual switch Hyper-V using PowerShell.

Create Virtual Switch Hyper-V Powershell

Prerequisites for creating a virtual switch in Hyper-V

Before creating and configuring a virtual switch in Hyper-V, your computer must meet the following prerequisites:

✔ Install Hyper-V and enable it on your Windows machine.

✔ Determine what type of virtual switch you need to create.

✔ Verify that the physical NIC intended for the external virtual switch is available and not in use by another configuration.

✔ PowerShell must be run as an administrator.

How to create a virtual switch using PowerShell step by step

You can choose the GUI method via Hyper-V Manager or PowerShell to create a Hyper-V virtual switch, the process is straightforward and efficient.

Using PowerShell to create a virtual switch with Hyper-V can improve efficiency and enable automated management. So, here are the specific steps:

1. Before creating a virtual switch, you need to check the available network adapters on the computer. Open PowerShell and enter the Get-NetAdapter command to obtain detailed information about all network adapters, including their names, statuses, and MAC addresses. This will help you determine which network adapter to bind to the virtual switch.

Get-NetAdapter

2. Create different types of virtual switches:

🟡Creating an external virtual switch with Hyper-V PowerShell:

If you want to create an external virtual switch to allow virtual machines to connect to external networks, such as the internet or the company's internal network, you can use the following command.

Assuming you want to create an external virtual switch named "ExternalSwitch" and bind it to a network adapter named "Ethernet0", you can run the command:

New-VMSwitch -Name "ExternalSwitch" -NetAdapterName "Ethernet0" -AllowManagementOS $true

Among them, the -Name parameter specifies the name of the virtual switch, the -NetAdapterName parameter specifies the name of the physical network adapter to be bound, and -AllowManagementOS $true indicates that the management operating system is allowed to share this network adapter.

🟣Create an internal virtual switch:

If you only need communication between virtual machines and between virtual machines and the host machine, you can create an internal virtual switch. Run the command:

New-VMSwitch -Name "InternalSwitch" -SwitchType Internal

Here, the -SwitchType Internal specifies the type of the virtual switch as internal.

🔵Create a private virtual switch:

For situations where communication is only needed between virtual machines and there is no desire to connect to external networks or the host machine, a private virtual switch can be created. Use the command:

New-VMSwitch -Name "PrivateSwitch" -SwitchType Private

3. You can use the Get-VMSwitch command to view detailed information about all the virtual switches created on the Hyper-V host, including their names, types, statuses, etc.

4. If you need to modify the settings of a virtual switch, such as allowing the management operating system to share or setting VLAN identifiers, you can use the Set-VMSwitch command.

For example, to allow the management operating system to share the virtual switch named "ExternalSwitch," you can run

Set-VMSwitch -Name "ExternalSwitch" -AllowManagementOS $true

5. If a virtual switch is no longer needed, you can use the Remove-VMSwitch command to delete it.

For example, to delete a virtual switch named "InternalSwitch," you can run the command:

Remove-VMSwitch -Name "InternalSwitch"

Important: Always backup Hyper-V virtual machines

After building the Hyper-V environment and creating the virtual switch, data security and virtual machine backup are crucial steps. AOMEI Cyber Backup is a powerful software specifically designed for virtual machine backups, and it offers many features:

  • Support multiple backup strategies
  • Automated backup tasks Instantly restore the virtual machine
  • Intuitive interface and centralized management
Download FreewareVMware ESXi and Hyper-V
Secure Download

How to backup and restore Hyper-V VMs (3 easy steps)

1. Bind Devices: Access to AOMEI Cyber Backup web client, navigate to Source Device > Hyper-V > + Add Hyper-V to add a Hyper-V host. And then click > Bind Device.

Add Hyper-V Host

2. Create Backup Task: Navigate to Backup Task > + Create New Task, and then set Task Name, Backup Type, Device, Target, and Schedule.

  • Device: Select VMs on the host in one backup task.
  • Target: Select to back up to a local path, or to a network path.
  • Schedule: Choose to perform full, differential or incremental backup, and automate execution according to the frequency you specified.

Schedule Hyper-V VM Backup

3. Run Backup: Click Start Backup and select Add the schedule and start backup now, or Add the schedule only.

Start Backup

4. Restore VM: Click > Restore on the backup task to open a wizard, and click Select Content to specify a restore point. Select to Restore to original location, and click Start Restore.

Restore Hyper-V VM

FAQ about Hyper-V virtual switch

Q: What is the purpose of VLAN tagging in Hyper-V?

VLAN tagging segregates network traffic by assigning VLAN IDs to virtual switches, enhancing network security and organization.

Q: Why am I unable to create a virtual switch using my network adapter?

The adapter might already be in use or lack support for virtual switch creation. Use Get-NetAdapter to verify.

Q: Can I create multiple virtual switches on the same adapter?

No, each adapter can only be bound to one virtual switch at a time.

Q: Do I need a physical NIC for all types of virtual switches?

No, only external switches require a physical NIC. Internal and private switches are entirely virtual.

Q: Can I create multiple virtual switches on a single host?

Yes, you can create multiple switches, but each external switch must use a different physical network adapter.

Conclusion

Creating a virtual switch in Hyper-V using PowerShell is a powerful way to streamline virtual networking. By leveraging commands like New-VMSwitch and Set-VMSwitch, you can build customized network configurations that meet your environment's needs. Whether you're setting up a test lab or managing enterprise-scale virtualization, PowerShell offers the flexibility and efficiency to optimize your workflow.

Crystal
Crystal · Editor
Crystal is an editor from AOMEI Technology. She mainly writes articles about virtual machine. She is a positive young lady likes to share articles with peolpe. Off work she loves travelling and cooking which is wonderful for life.