{"id":2751,"date":"2013-10-09T00:01:00","date_gmt":"2013-10-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/09\/use-powershell-to-create-virtual-switches\/"},"modified":"2013-10-09T00:01:00","modified_gmt":"2013-10-09T00:01:00","slug":"use-powershell-to-create-virtual-switches","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-virtual-switches\/","title":{"rendered":"Use PowerShell to Create Virtual Switches"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to use Windows PowerShell to easily create virtual switches in Hyper-V.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are really looking forward to going to Atlanta to <a href=\"http:\/\/powershellsaturday.com\/005\/\" target=\"_blank\">Windows PowerShell Saturday 005<\/a>. 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.)<\/p>\n<p>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:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5531.HSG-10-9-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5531.HSG-10-9-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>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:<\/p>\n<ul>\n<li>Ethernet external access<\/li>\n<li>Wireless external access<\/li>\n<li>Internal switch<\/li>\n<li>Private switch<\/li>\n<\/ul>\n<p>So, that means encountering a veritable click-fest of mousing around&mdash;and that is something that a Scripting Guy just cannot abide. Luckily, I can use Windows PowerShell to solve my dilemma. WooHoo!<\/p>\n<h2>Find existing network adapters<\/h2>\n<p>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 <strong>Get-NetAdapter<\/strong> cmdlet. This command is shown here, along with the associated output:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Windows\\System32\\WindowsPowerShell\\v1.0&gt; Get-NetAdapter<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InterfaceDescription&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ifIndex Status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MacAddress&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LinkSpeed<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;- &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">Ethernet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Intel(R) 82579LM Gigabit Network Con&#8230;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4 Disconnected 3C-97-0E-75-BD-88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 bps<\/p>\n<p style=\"padding-left: 30px\">Wi-Fi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Intel(R) Centrino(R) Ultimate-N 6300&#8230;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3 Up&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24-77-03-CF-43-48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 29 Mbps<\/p>\n<p style=\"padding-left: 30px\">Bluetooth Network Conn&#8230; Bluetooth Device (Personal Area Netw&#8230;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8 Not Present&nbsp; 9C-2A-70-82-E4-3F&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 bps<\/p>\n<p>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&nbsp;8 names the adapters with realistic, descriptive names: Ethernet and Wi-Fi. This is so much better than the previous &ldquo;Local Area Connection&rdquo; pattern we used to encounter. So, now I know what network adapters I have and their associated names. Groovy!<\/p>\n<h2>Create the virtual switches<\/h2>\n<p>I can use the <strong>Get-VMSwitch<\/strong> 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 <strong>New-VMSwitch<\/strong> command is a single-line command that is wrapped for readability, but is not broken with any line continuation characters):<\/p>\n<p style=\"padding-left: 30px\">&lt;#<\/p>\n<p style=\"padding-left: 30px\">&nbsp;CreateHyperVSwitches.ps1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\">&nbsp;hsg-10-9-13<\/p>\n<p style=\"padding-left: 30px\">#&gt;<\/p>\n<p style=\"padding-left: 30px\">Import-Module Hyper-V<\/p>\n<p style=\"padding-left: 30px\">$ethernet = Get-NetAdapter -Name ethernet<\/p>\n<p style=\"padding-left: 30px\">$wifi = Get-NetAdapter -Name wi-fi<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">New-VMSwitch -Name externalSwitch -NetAdapterName $ethernet.Name -AllowManagementOS $true -Notes &#8216;Parent OS, VMs, LAN&#8217;<\/p>\n<p style=\"padding-left: 30px\">New-VMSwitch -Name WiFiExternalSwitch -NetAdapterName $wifi.Name -AllowManagementOS $true -Notes &#8216;Parent OS, VMs, wifi&#8217;<\/p>\n<p style=\"padding-left: 30px\">New-VMSwitch -Name privateSwitch -SwitchType Private -Notes &#8216;Internal VMs only&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">New-VMSwitch -Name internalSwitch -SwitchType Internal -Notes &#8216;Parent OS, and internal VMs&#8217;<\/p>\n<p>When I run the script, it creates all four virtual switches on my system, and the command pane shows the output results shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0003.HSG-10-9-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0003.HSG-10-9-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I go back to the Virtual Switch Manager, and I can see my four newly created virtual switches.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7331.HSG-10-9-13-03.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7331.HSG-10-9-13-03.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>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!<\/p>\n<p>Join me tomorrow when we will have a guest blog post by Richard Siddaway as he continues his Admin&rsquo;s First Steps series.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[458,271,3,45],"class_list":["post-2751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-applications","tag-hyper-v","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2751"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2751\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}