{"id":5023,"date":"2012-09-05T00:01:00","date_gmt":"2012-09-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/05\/use-powershell-3-0-to-stop-network-adapters-on-windows-8\/"},"modified":"2012-09-05T00:01:00","modified_gmt":"2012-09-05T00:01:00","slug":"use-powershell-3-0-to-stop-network-adapters-on-windows-8","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-3-0-to-stop-network-adapters-on-windows-8\/","title":{"rendered":"Use PowerShell 3.0 to Stop Network Adapters on Windows 8"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to stop network adapters on Windows&nbsp;8.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The other day a customer asked me, &ldquo;Is it a Microsoft requirement that all their consultants can&rsquo;t be more than five miles from a coffee house?&rdquo; Perhaps a little bit of background is in order. I was onsite with a large Microsoft premier customer, delivering a Windows PowerShell class. Their TAM, Robert, is one of my good friends at Microsoft, and I have worked with him for over a decade. Because I miss my teapot and my stash of teas, I developed the habit of stopping by the coffee house after lunch and getting a large cup of green tea&mdash;they actually have a pretty nice green tea with spearmint and lemon grass in it. They had no monkey-picked and no jasmine; but after all, it is a coffee house and not a tearoom. I was happy with what I could obtain. I looked around, and all of the Microsoft people (there were other consultants working on various projects as well) had similar looking large cups of coffee. And thus the question. Although both Microsoft and this particular coffee house chain are based in Seattle, as far as I know, there is no other relationship&mdash;and most certainly, there is no proximity-based requirement.<\/p>\n<h2>Stopping network adapters<\/h2>\n<p>Windows Vista introduced the ability to stop a network adapter by using the Win32_NetworkAdapter WMI class. In the following example, I use the <b>Get-CimClass<\/b> cmdlet from Windows PowerShell&nbsp;3.0 to return <b>cimclassmethods<\/b> from the Win32_NetworkAdapter WMI class. I use the dotted notation and automatic <b>foreach<\/b> technique to pipe the resulting class methods to <b>Where-Object<\/b> (<b>?<\/b> is the alias), where I use the simplified <b>Where-Object<\/b> syntax from Windows PowerShell&nbsp;3.0 to find only the implemented <b>cimclassmethods<\/b>. (For more information about these types of Windows PowerShell&nbsp;3.0 syntax tricks, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/20\/my-five-favorite-powershell-3-0-tips-and-tricks.aspx\" target=\"_blank\">My Five Favorite PowerShell 3.0 Tips and Tricks<\/a>.)<i> <\/i>The Win32_NetworkAdapter class methods are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-CimClass win32_networkadapter).cimclassmethods | ? qualifiers -match implemented<\/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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReturnType Parameters&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Qualifiers<\/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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Enable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UInt32 {}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {Implemented, Map&#8230;<\/p>\n<p style=\"padding-left: 30px\">Disable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UInt32 {}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {Implemented, Map&#8230;<\/p>\n<p>In Windows&nbsp;8 and Windows Server 2012, I can use Windows PowerShell&nbsp;3.0 to stop or to start a network adapter by using one of the CIM commands. Of course, the function wraps the WMI class, but it also makes things really easy. The <b>netadapter<\/b><i> <\/i>functions are shown here (<b>gcm<\/b> is an alias for the <b>Get-Command<\/b> cmdlet.)<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gcm -Noun netadapter | select name, modulename<\/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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ModuleName<\/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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Disable-NetAdapter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetAdapter<\/p>\n<p style=\"padding-left: 30px\">Enable-NetAdapter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NetAdapter<\/p>\n<p style=\"padding-left: 30px\">Get-NetAdapter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetAdapter<\/p>\n<p style=\"padding-left: 30px\">Rename-NetAdapter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetAdapter<\/p>\n<p style=\"padding-left: 30px\">Restart-NetAdapter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetAdapter<\/p>\n<p style=\"padding-left: 30px\">Set-NetAdapter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetAdapter<\/p>\n<p><b>Note&nbsp;&nbsp;&nbsp;<\/b>To enable or disable network adapters requires admin rights. Therefore, you must start the Windows PowerShell console with an account that has rights to perform the task.<\/p>\n<p>The various network adapters on my laptop are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7573.HSG-9-5-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7573.HSG-9-5-12-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>I do not like having enabled, disconnected network adapters. Instead, I prefer to only enable the network adapter that I am using. (There are a number of reasons for this, such as simplified routing tables, performance issues, and security concerns.) In the past, I wrote a script. Now I only need to use a Windows PowerShell command. If I only want to disable the non-connected network adapters, the command is easy. It is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-NetAdapter | ? status -ne up | Disable-NetAdapter<\/p>\n<p>The problem with the previous command is that it prompts as shown in the following image. This is not much fun when there are multiple network adapters to disable.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.HSG-9-5-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.HSG-9-5-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To suppress the prompt, I need to supply <b>$false<\/b> to the <b>&ndash;confirm<\/b> parameter. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-NetAdapter | ? status -ne up | Disable-NetAdapter -Confirm:$false<\/p>\n<p>A quick check in Control Panel shows the disconnected adapters are now disabled. This appears here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7318.HSG-9-5-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7318.HSG-9-5-12-03.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>If I want to enable a specific network adapter, I use the <b>Enable-Network<\/b> adapter. I can specify by name as shown here.<\/p>\n<p style=\"padding-left: 30px\">Enable-NetAdapter -Name ethernet -Confirm:$false<\/p>\n<p>If I do not want to type the adapter name, I can use the <b>Get-NetAdapter<\/b> cmdlet to retrieve a specific network adapter and then enable it as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-NetAdapter -Name vethernet* | ? status -eq disabled | Enable-NetAdapter -Confirm:$false<\/p>\n<p>It is also possible to use wildcard characters with the <b>Get-NetAdapter<\/b> to retrieve multiple adapters, and pipe them directly to the <b>Disable-NetAdapter<\/b> cmdlet. The following code permits the confirmation prompts so that I can selectively enable or disable the adapter as I wish.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-NetAdapter -Name vethernet* | Disable-NetAdapter<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Confirm<\/p>\n<p style=\"padding-left: 30px\">Are you sure you want to perform this action?<\/p>\n<p style=\"padding-left: 30px\">Disable-NetAdapter &#8216;vEthernet (InternalSwitch)&#8217;<\/p>\n<p style=\"padding-left: 30px\">[Y] Yes&nbsp; [A] Yes to All&nbsp; [N] No&nbsp; [L] No to All&nbsp; [S] Suspend&nbsp; [?] Help<\/p>\n<p style=\"padding-left: 30px\">(default is &#8220;Y&#8221;):y<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Confirm<\/p>\n<p style=\"padding-left: 30px\">Are you sure you want to perform this action?<\/p>\n<p style=\"padding-left: 30px\">Disable-NetAdapter &#8216;vEthernet (ExternalSwitch)&#8217;<\/p>\n<p style=\"padding-left: 30px\">[Y] Yes&nbsp; [A] Yes to All&nbsp; [N] No&nbsp; [L] No to All &nbsp;[S] Suspend&nbsp; [?] Help<\/p>\n<p style=\"padding-left: 30px\">(default is &#8220;Y&#8221;):n<\/p>\n<p>That is about it for now. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/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><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to stop network adapters on Windows&nbsp;8. Microsoft Scripting Guy, Ed Wilson, is here. The other day a customer asked me, &ldquo;Is it a Microsoft requirement that all their consultants can&rsquo;t be more than five miles from a coffee house?&rdquo; Perhaps a little bit [&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":[34,37,362,3,367,45],"class_list":["post-5023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hardware","tag-networking","tag-powershell-3","tag-scripting-guy","tag-windows-8","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to stop network adapters on Windows&nbsp;8. Microsoft Scripting Guy, Ed Wilson, is here. The other day a customer asked me, &ldquo;Is it a Microsoft requirement that all their consultants can&rsquo;t be more than five miles from a coffee house?&rdquo; Perhaps a little bit [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5023","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=5023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5023\/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=5023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}