大网数据广告活动
Hyper-V与虚拟化

Create and configure a virtual switch with Hyper-V

· 52CE 我爱测

Learn how to create and configure a virtual switch for use by the Hyper-V host and guest virtual machines.

This article shows you how to create and configure your virtual switch using Hyper-V Manager or PowerShell. A virtual switch allows virtual machines created on Hyper-V hosts to communicate with other computers.

When you first install the Hyper-V role on Windows Server, you can optionally create a virtual switch at the same time. To learn more about virtual switches, see Hyper-V Virtual Switch.

For more information about how you can set up your networking infrastructure with Windows Server, review the Networking documentation.

Prerequisites

Before you can create and configure your virtual switch, your computer must meet the following prerequisites:

Create a virtual switch

Once you complete the prerequisites, you're ready to create your virtual switch. In this section, we create a basic virtual switch by following these steps.

Hyper-V Manager

Here's how to create a virtual switch using Hyper-V Manager.

  1. Open Hyper-V Manager.

  2. From the Actions pane, select Virtual Switch Manager.

  3. Choose the type of virtual switch, then select Create Virtual Switch.

  4. Enter a name for the virtual switch, then perform one of the following steps.

    1. If you selected external, choose the network adapter (NIC) that you want to use, then select OK.

      You're prompted with a warning that the change may disrupt your network connectivity; select Yes if you're happy to continue.

    2. If you selected internal or private, select OK.

PowerShell

Here's how to create a virtual switch using the New-VMSwitch command.

  1. Run PowerShell on your computer in an elevated session.

  2. Find existing network adapters by running the Get-NetAdapter cmdlet. Identify the network adapter name that you want to use for the virtual switch.

    Get-NetAdapter
    
  3. To create an external virtual switch, run the following command, replacing the placeholders <switch-name> and <netadapter-name> with your own values.

    New-VMSwitch -Name <switch-name>  -NetAdapterName <netadapter-name>
    

    Use either the parameter NetAdapterName, or NetAdapterInterfaceDescription to implicitly set the type of the virtual switch to external.

  4. To create an internal or private switch, run the following command. Replace <switch-name> with the name of your switch and <switch-type> with either Internal or Private.

    New-VMSwitch -Name <switch-name> -SwitchType <switch-type>
    

For more advanced PowerShell scripts that cover improved or new virtual switch features in Windows Server, see Remote Direct Memory Access and Switch Embedded Teaming.


Management Operating System sharing

An external virtual switch allows your virtual machines to connect to an external network. You can also allow the management operating system to share the same selected network adapter. To begin, follow these steps.

Hyper-V Manager

Here's how to allow the management operating system to share the selected network adapter switch using Hyper-V Manager.

  1. Open Hyper-V Manager.

  2. From the Actions pane, select Virtual Switch Manager.

  3. Select the virtual switch you wish to configure, check the Allow management operating system to share this network adapter and select OK.

    You'll be prompted with a warning that the change may disrupt your network connectivity; select Yes if you're happy to continue.

PowerShell

Here's how to allow a virtual switch to be shared with the management OS using the Set-VMSwitch command.

  1. Run PowerShell on your computer in an elevated session.

  2. To allow a virtual switch to be shared with the management OS, run the following commands. Replace the placeholder <vmswitch name> the name of your virtual switch.

    Set-VMSwitch -Name <vmswitch name> -AllowManagementOS $true
    

Virtual LAN (VLAN) identification

You can specify the VLAN identification (ID) used by virtual machines network adapters and virtual switches. For virtual switches connected to either an external or internal network, you can specify the (VLAN) ID. The VLAN ID number is used by the management operating system and virtual machines communicating through this virtual switch.

You can also configure your virtual switch with other VLAN options, such port mode and the native VLAN ID. For these options, you need to use PowerShell and ensure the configuration is compatible with your networks configuration.

To configure the VLAN identification for the switch, follow these steps.

Hyper-V Manager

Here's how to specify the VLAN ID using the Hyper-V Manager.

  1. Open Hyper-V Manager.

  2. From the Actions pane, select Virtual Switch Manager.

  3. Select the virtual switch you wish to configure, check the Enable virtual LAN identification for management operating system.

    1. You can enter any VLAN ID number or leave the default, then select OK.

      You're prompted to warn you that the change may disrupt your network connectivity, select Yes if you're happy to continue.

VLAN identifiers should be consistent with your network to ensure compatibility between your computer, virtual machines, and other network devices.

PowerShell

Here's how to set a VLAN identification using the Set-VMNetworkAdapterVlan command.

  1. Run PowerShell on your computer in an elevated session.

  2. To set the VLAN ID to tag traffic and set the port mode to access, run the following commands. Replace the placeholder <vmswitch name> the name of your virtual switch and <id> with your VLAN ID.

    Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName <vmswitch name> -Access -VlanId <id>
    

    Or

    To set port mode to trunk, the native VLAN ID, and allowed VLAN ID list, run the following commands. Replace the placeholder <vmswitch name> the name of your virtual switch and <id> with your VLAN ID. Replace <allowed ids> with a list of allowed VLAN IDs, for example, 200-210.

    $parameters = @{
         ManagementOS = $true
         VMNetworkAdapterName = <vmswitch name>
         Trunk = $true
         NativeVlanId = <id>
         AllowedVlanIdList = <allowed ids>
    }
    Set-VMNetworkAdapterVlan @parameters
    

This example uses splatting to pass parameter values from the $parameters variable to the command. Learn more about Splatting.


NAT networking

Network Address Translation (NAT) gives a virtual machine access to your computer's network by combining the host computer's IP address with a port through an internal Hyper-V Virtual Switch.

This has a few useful properties:

  1. NAT Conserves IP addresses by mapping an external IP address and port to a much larger set of internal IP addresses.
  2. NAT allows multiple virtual machines to host applications that require identical (internal) communication ports by mapping these to unique external ports.
  3. NAT uses an internal switch -- creating an internal switch doesn't cause you to use network connection and tends to interfere less with a computer's networking.

To set up a NAT network and connect it to a virtual machine, follow the NAT networking user guide.

Related content

Now that you set up a virtual switch, here are other articles to help you continue with Hyper-V.

Source: Microsoft Learn / Windows Server documentation by Microsoft and contributors (upstream author metadata: robinharwood). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Server documentation
适用范围
Windows Server · Source date 01/29/2025
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。