Create a Virtual Switch for Hyper-V with PowerShell

Spread the words

Virtual switch on Hyper-V could be configured with one of these three connections type.

  • External Network: connected to a physical network. Allows the VMs to use your real NIC to communicate with the rest of the world just like a regular computer on your network would.
  • Internal Network: not connected to a physical network. Lets the VMs on the server communicate with each other, and the server.
  • Private network: not connected to a physical network. Lets the VMs on the server communicate with each other.

Use the Get-NetAdapter cmdlet to get the list of network adapters connected to the Host.

Get-NetAdapter

Netadapter

Now use the New-VMSwitch cmdlet to create the virtual switch.

Here I’m creating an External Network with a switch named External VM Switch.

The AllowManagementOS parameter specifies whether the management OS is to have access to the physical NIC bound to the virtual switch to be created.

The Name parameter specifies the name of the switch to be created.

The NetAdapterName parameter specifies the name of the network adapter to be bound to the switch to be created. When you use NetAdapterName parameter, it implicitly set the type of the virtual switch to External.

New-VMSwitch –Name “External VM Switch” –AllowManagement $True –NetAdapterName “Local Area Connection”

newvmswitch

You can use Get-VMSwitch cmdlet to get the status of the vm switch.

getvmswitch

The first virtual switch is here.

vswitch

To remove the vm switch, use the Remove-VMSwitch cmdlet.

Remove-VMSwitch “External VM Switch”

removevmswitch

🙂

2 Comments on "Create a Virtual Switch for Hyper-V with PowerShell"

  1. Which version of PowerShell are you using, on Windows 7 + PowerShell 5, I don’t have the Get-NetAdapter command

    Get-Netadapter : The term ‘Get-Netadapter’ is not recognized as the name of a cmdlet, function, script file, or
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
    again.
    At line:1 char:1
    + Get-Netadapter
    + ~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-Netadapter:String) [], CommandNotFoundException

    • FrenchITGuy | 07/09/2016 at 10:40 am | Reply

      Hi,
      The get-netadapter cmdlet is working only on Windows 8.1, Windows server 2012 and up. Even if you install the latest version of powershell in Windows 7, it wouldn’t work. Many of the PowerShell modules rely on OS-specific WMI namespaces. As these are not present in Windows 7, the modules cannot be imported.

Leave a Reply to Simon Cancel reply

Your email address will not be published.


*