Use PowerShell to Create Virtual Switches

Doctor Scripto

Summary: Learn how to use Windows PowerShell to easily create virtual switches in Hyper-V.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are really looking forward to going to Atlanta to Windows PowerShell Saturday 005. It will be held at the Microsoft Office in Alpharetta, Georgia on October 26, 2013. There are still some tickets available for this event, and it will absolutely rock. I am doing two presentations, and there is a stellar collection of other speakers. The Scripting Wife will be there helping out with registration and other behind-the-scenes activities. Yes, it will be a great way to spend a lazy fall Saturday. You should register before the event sells out. (All of the other Windows PowerShell Saturday events have sold out.)

It is easy to create a virtual switch in Hyper-V Manager. All that the task requires is to open Hyper-V Manager, click Virtual Switch Manager, follow a couple of wizard screens, and choose from two dozen options. The Virtual Switch Manager Wizard is shown here:

Image of menu

This is not too bad if all I need to do is to create a single virtual switch on a single computer (such as my laptop). But on my laptop, I generally create at least the following four virtual switches:

  • Ethernet external access
  • Wireless external access
  • Internal switch
  • Private switch

So, that means encountering a veritable click-fest of mousing around—and that is something that a Scripting Guy just cannot abide. Luckily, I can use Windows PowerShell to solve my dilemma. WooHoo!

Find existing network adapters

First I need to identify what network adapters I have on my laptop. I am talking real, live, physical network adapters, not the virtual stuff. This is because I want at least some of my virtual machines to be able to talk to the real world. To inspect my current network adapters, I use the Get-NetAdapter cmdlet. This command is shown here, along with the associated output:

PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-NetAdapter

 

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed

—-                      ——————–                    ——- ——       ———-             ———

Ethernet                  Intel(R) 82579LM Gigabit Network Con…       4 Disconnected 3C-97-0E-75-BD-88          0 bps

Wi-Fi                     Intel(R) Centrino(R) Ultimate-N 6300…       3 Up           24-77-03-CF-43-48        29 Mbps

Bluetooth Network Conn… Bluetooth Device (Personal Area Netw…       8 Not Present  9C-2A-70-82-E4-3F          0 bps

Basically, I see that I have three network adapters. The Bluetooth Personal Area Network adapter, I have used less than a dozen times in as many years. The other two, I consider to be my main adapters. Luckily, Windows 8 names the adapters with realistic, descriptive names: Ethernet and Wi-Fi. This is so much better than the previous “Local Area Connection” pattern we used to encounter. So, now I know what network adapters I have and their associated names. Groovy!

Create the virtual switches

I can use the Get-VMSwitch cmdlet to check what virtual switches may already exist. When I run it on my system, nothing returns, so I have a wide open slate. I decide to write a quick script to create all of the virtual switches at the same time. The script is shown here (each New-VMSwitch command is a single-line command that is wrapped for readability, but is not broken with any line continuation characters):

<#

 CreateHyperVSwitches.ps1

 ed wilson, msft

 hsg-10-9-13

#>

Import-Module Hyper-V

$ethernet = Get-NetAdapter -Name ethernet

$wifi = Get-NetAdapter -Name wi-fi

 

New-VMSwitch -Name externalSwitch -NetAdapterName $ethernet.Name -AllowManagementOS $true -Notes ‘Parent OS, VMs, LAN’

New-VMSwitch -Name WiFiExternalSwitch -NetAdapterName $wifi.Name -AllowManagementOS $true -Notes ‘Parent OS, VMs, wifi’

New-VMSwitch -Name privateSwitch -SwitchType Private -Notes ‘Internal VMs only’

 

New-VMSwitch -Name internalSwitch -SwitchType Internal -Notes ‘Parent OS, and internal VMs’

When I run the script, it creates all four virtual switches on my system, and the command pane shows the output results shown here:

Image of command output

I go back to the Virtual Switch Manager, and I can see my four newly created virtual switches.

Image of menu

Sweet. It took less than two minutes to write the script (less time than it would take to create the virtual switches one-at-a time), and I have a script that I can also use on my remote servers. Windows PowerShell for the win!

Join me tomorrow when we will have a guest blog post by Richard Siddaway as he continues his Admin’s First Steps series.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon