Creating Virtual Switches with PowerShell

Problem: Hyper-V virtual switches on Windows 10 Technical Preview seem to become unstable or stop working after a new revision is installed

Solution:  Use PowerShell to remove and re-add, lets explore…

Creating virtual switches is easy in the GUI, so why would I use PowerShell?  Well, it’s OK saying use the GUI if all you’re doing is creating a few virtual switches for a lab environment, in large production environments with varying infrastructures, VLANs and other environments you may need to harness the power of PowerShell to speed things up so its probably important to learn these commands regardless.

I’ve noticed that sometimes my virtual switches seem to become unstable or behave strangely after each Windows 10 Technical Preview release but if I remove and re-create them things seem to go back to being OK.  This isn’t the case with every revision however I recently upgraded to technical preview 10122 and have had no end of trouble.  For me its the most frustrating release yet.  In my experience Networking and Sharing Center refuses to open, Windows Update locks up & does nothing, Internet Explorer crashes constantly (I now use Spartan/Edge however it too is not without flaws) and other problems I don’t care to mention.

So lets have a look at how I broke down the PowerShell code and found the commands I needed.  Here is a list of virtual switches I use on my laptop:

1. External Switch (bound to the Ethernet adapter)
2. External Switch (bound to the Wi-Fi adapter)
3. Private Switch – New York
4. Private Switch – Stockholm
5. Private Switch – Liverpool
6. Private Switch – MDT Testing

So lets start by examining PowerShell for commands with the word ‘switch’ in them.  By passing Get-Command *switch* I get a quite a few results but thankfully there’s some obvious commands in the module Hyper-V these actually use the ‘VMSwitch’ rather than ‘switch’ so I try Get-Command *vmswitch* instead.

image

So I guess the obvious ones to try are Get-VMSwitch and New-VMSwitch, so lets explore those.

Get-VMSwitch  returns…

image

I want to remove these and re-create them as I’m experiencing trouble using them and strange behaviour on Windows 10 Technical Preview 10122.

Can I use Get-VMSwitch | Remove-VMSwtich to remove them all?  Lets see…

image

Get-VMSwitch results now?

image

So it removed some but not all…  so what happens if I try the command individually instead of piping it.  So lets try Remove-VMSwitch –Name “External – LAN”

image

Yep, worked fine but it asked me to confirm, so can I try a –Force command? Yes I can! (It worked!) so rinse and repeat with the remaining 3..

Remove-VMSwitch –Name “External – WLAN” –Force
Remove-VMSwitch –Name “Private – New York” –Force
Remove-VMSwitch –Name “External – Stockholm” –Force

Get-VMSwitch now returns no results!  Not sure why I couldn’t pipe the results to remove them all in one go.  Ah well, it is a technical preview right?

Now lets explore New-VMSwitch.  I try Get-Help New-VMSwitch –examples, this resulted in no help whatsoever so I tried Update-Help which is suggested which strangely failed.  Twice.

image

Fine, whatever PowerShell, so what I’ll do is Type New-VMSwitch and see what happens…

image

First it asks for a Name (simple enough, “Test” it is) then it asks for a NetAdapterName, quickly fire up another PowerShell console and type Get-NetAdapter this tells me my LAN port is named “Ethernet” so I try that.  It then asks me for another NetAdapterName (strange) which I don’t need so I hit return.  It seems to do something.  I try Get-VMSwitch and I receive the following:

image

Let’s have a look at the properties of this virtual switch and see what we might be able to pass to the command New-VMSwitch.  To do this I pass the following command Get-VMSwitch | Format-List * to show all the properties of it.

image

From that list I think name and switch type are important.  We know from testing the New-VMSwitch command that NetAdapterName is asked too.  Now lets try to piece together a command and see what happens.  I have removed the Test VMSwitch before I start.  So I’m going to try this.

New-VMSwitch –Name “External – WLAN” –SwitchType External -NetAdapterName Wi-Fi … but I got an error…

image

Seems that I can only specify “Internal” or “Private” with my –SwitchType parameter.  So lets remove it and try New-VMSwitch –Name “External – WLAN” -NetAdapterName Wi-Fi

SUCCESS!

Repeat for external LAN: New-VMSwitch –Name “External – LAN” –NetAdapterName Ethernet

SUCCESS!

Next, the Private Switches (remember I don’t need to specify a –NetAdapterName as its not getting bound to a network card and I must specify a SwitchType of Private), I try this:

New-VMSwitch –Name “Private – New York” –SwitchType Private  - SUCCESS!  Repeat for the others…
New-VMSwitch –Name “Private – Stockholm” –SwitchType Private
New-VMSwitch –Name “Private – Liverpool” –SwitchType Private
New-VMSwitch –Name “Private – MDT Testing” –SwitchType Private

So I run Get-VMSwitch  to see what’s going on…

image

WHOO!

Now I can put a script together to take care of this for me after the next update, should I experience problems with the next revision. I think I’ll just use a list of individual commands rather that a get all and pipe all to remove command, just so that I can be certain it works.  I’ll probably look into this further down the line, revisit the script and retest when the actual release comes out but I think even the full release will have some issues. 

Hope this helps some of you.

Jonathan.

Add comment