{"id":55053,"date":"2008-09-29T11:04:00","date_gmt":"2008-09-29T11:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/09\/29\/hey-scripting-guy-how-can-i-enable-or-disable-my-network-adapter\/"},"modified":"2008-09-29T11:04:00","modified_gmt":"2008-09-29T11:04:00","slug":"hey-scripting-guy-how-can-i-enable-or-disable-my-network-adapter","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-enable-or-disable-my-network-adapter\/","title":{"rendered":"Hey, Scripting Guy! How Can I Enable or Disable My Network Adapter?"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! At my office I access the corporate network by using a wired ethernet connection. However, when I come home, I have a wireless connection. Since our security guys do not allow us to have multiple network connections active at the same time, I am constantly enabling the wireless connection when I get home, and disabling the wireless connection when I get to the office. In Windows Vista, this seems like it takes twelve clicks. Is this something that I can script?<\/p>\n<p>&#8211; TB<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hi TB,<\/p>\n<p>Sounds like your security guys are on top of things. One of the major ways malware weasels its way into networks is through unsecured wireless access. If they allow you to bridge the wired network with some unsecured wireless access point that is blazing away at the local hamburger shop, they would not be doing their job. It is the same thing as buying a really good lock for the front door of your house and leaving the windows open. You were complaining about twelve clicks to enable or disable the network adapter (I have not counted them, so I will just take your word for it), but the good thing about running Windows Vista is that we have upgraded the <b>Win32_NetworkAdapter<\/b> WMI class. <\/p>\n<p>In Windows Vista, there is an enable and a disable method for the network adapter. To use this in a script, all we need to do is select the specific network card we wish to work with, and then call the appropriate method. This is seen in this&nbsp;script: <\/p>\n<pre class=\"codeSample\">Param(\n      $wireless,\n      $wired,\n      [switch]$help\n      )\nFunction GetHelp()\n{\n  $helpText= `\n@\"\n DESCRIPTION:\n NAME: EnableDisableNetworkAdapters.ps1\n Enables or Disables Network Adapters on a local machine. \n\n PARAMETERS: \n -wireless &lt;enable, disable, query&gt;\n -wired &lt;enable, disable, query&gt;\n -Help displays this help topic\n SYNTAX:\n EnableDisableNetworkAdapters.ps1 -wired query\n Reports basic information about the wired network adapter\n EnableDisableNetworkAdapters.ps1 -wired query -wireless query\n Reports basic information about both the wired and wireless network adapters\n EnableDisableNetworkAdapters.ps1 -wired disable\n Disables the wired network adapter (Requires Admin rights)\n EnableDisableNetworkAdapters.ps1 -wired disable -wireless enable\n Disables the wired network adapter and enables the wireless network adapter (Requires Admin rights)\n EnableDisableNetworkAdapters.ps1 -help\n Prints the help topic for the script\n\"@ #end helpText\n  $helpText\n} #end GetHelp\nFunction GetWireless()\n{\n $filter = \"Name LIKE '%Wireless%'\"\n NetworkAdapterFactory($wireless)\n}#end GetWireless\nFunction GetWired()\n{\n $filter = \"Name LIKE '%GigaBit%'\"\n NetworkAdapterFactory($wired)\n} #end GetWired\nFunction NetworkAdapterFactory($adapter)\n{\n $wmi = Get-WmiObject -Class Win32_NetworkAdapter `\n -filter $filter\n switch ($adapter)\n {\n  \"query\" { $wmi }\n  \"enable\" { $wmi.enable() }\n  \"disable\" { $wmi.disable() }\n  DEFAULT { \n           Write-Host -foregroundcolor red \"unrecognized parameter.\" \n           GetHelp \n           Exit \n          }\n } #end switch\n} #end NetWorkAdapterFactory\n# --- ENTRY POINT ---\nIf($help) { GetHelp ; exit }\nif($wireless) { GetWireless }\nif($wired) { GetWired }\nIf(!$wireless -or !$wired) { GetHelp ; exit }<\/pre>\n<p>We begin the script by using the <b>param<\/b> statement to enable the script to accept command line parameters. We create three parameters: <b>$wireless<\/b>, <b>$wired<\/b>, and <b>$help<\/b>. The <b>$help<\/b> parameter is a switched parameter, and will effect the script only if it is supplied from the command line. The other two parameters allow us to work with either the wireless network card or the wired network card. This section of the code is shown&nbsp;here: <\/p>\n<pre class=\"codeSample\">Param(\n      $wireless,\n      $wired,\n      [switch]$help\n      ) <\/pre>\n<p>The next thing we do is create a function that will display the Help for the script. This function uses a <b>here<\/b> string to allow us to type in a bunch of text when we were creating the script. To create a <b>here<\/b> string, you begin a line with an at sign (@) followed by a quotation mark. You end the <b>here<\/b> string in the opposite manner. You want to assign the <b>here<\/b> string to a variable. This syntax is shown&nbsp;here: <\/p>\n<pre class=\"codeSample\">$hereString = @\"\nThis is a string\n\"@<\/pre>\n<p>The style of the <b>GetHelp<\/b> function follows the style of the Help that is displayed when using the <b>GetHelp<\/b> cmdlet. The Help text is divided into three sections: <b>Description<\/b>, <b>Parameters<\/b>, and <b>Syntax<\/b>. The <b>GetHelp<\/b> function is seen&nbsp;here: <\/p>\n<pre class=\"codeSample\">Function GetHelp()\n{\n  $helpText= `\n@\"\n DESCRIPTION:\n NAME: EnableDisableNetworkAdapters.ps1\n Enables or Disables Network Adapters on a local machine. \n\n PARAMETERS: \n -wireless &lt;enable, disable, query&gt;\n -wired &lt;enable, disable, query&gt;\n -Help displays this help topic\n\n SYNTAX:\n EnableDisableNetworkAdapters.ps1 -wired query\n Reports basic information about the wired network adapter\n\n EnableDisableNetworkAdapters.ps1 -wired query -wireless query\n Reports basic information about both the wired and wireless network adapters\n\n EnableDisableNetworkAdapters.ps1 -wired disable\n Disables the wired network adapter (Requires Admin rights)\n\n EnableDisableNetworkAdapters.ps1 -wired disable -wireless enable\n Disables the wired network adapter and enables the wireless network adapter (Requires Admin rights)\n\n EnableDisableNetworkAdapters.ps1 -help\n\n Prints the help topic for the script\n\"@ #end helpText\n  $helpText\n} #end GetHelp<\/pre>\n<p>When the script displays the Help text information, it prints out rather nicely. The <b>GetHelp<\/b> output is shown here: <\/p>\n<p><img decoding=\"async\" height=\"375\" alt=\"GetHelp output graphic\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey0922\/enabledisablenichelp.jpg\" width=\"500\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>The next thing we do in the script is create two functions. These functions are very similar to each other. They are called based upon the command line parameters. If you specify that you want to work with the wireless card, specifying the <b>-wireless<\/b> parameter calls the <b>GetWireless<\/b> function. It creates the filter for the WMI query and calls the <b>NetworkAdapterFactory<\/b> function while passing the value contained in the <b>$wireless<\/b> command line parameter. The <b>GetWired<\/b> function does exactly the same thing for the wired network adapter. If you specify <b>-wired<\/b> from the command line, this function gets called. One thing to note is that because of the design of the script, you are allowed to specify both <b>-wired<\/b> and <b>-wireless<\/b> from the same command line. This would allow you to enable one network connection and disable a second network connection with the same command. These two functions are shown&nbsp;: <\/p>\n<pre class=\"codeSample\">Function GetWireless()\n{\n $filter = \"Name LIKE '%Wireless%'\"\n NetworkAdapterFactory($wireless)\n}#end GetWireless\n\nFunction GetWired()\n{\n $filter = \"Name LIKE '%GigaBit%'\"\n NetworkAdapterFactory($wired)\n} #end GetWired<\/pre>\n<p>The main function in the script is the <b>NetworkAdapterFactory<\/b> function. The function accepts the filter that was created in either the <b>GetWired<\/b> or <b>GetWireless<\/b> functions. After the <b>Get-WmiObject<\/b> cmdlet returns the appropriate instance of the <b>Win32_NetworkAdapter<\/b> class, we store the resultant management object in the <b>$wmi<\/b> variable. We then pass the action to perform that was specified from the command line parameters. If we want to query the details about the network adapter, the <b>switch<\/b> statement displays the information contained in the <b>$wmi<\/b> variable. If we wish to enable the network adapter, we call the <b>enable<\/b> method. If <b>disable<\/b> was specified when the script was launched, we call the <b>disable<\/b> method. If there are no matches in the <b>switch<\/b> statement, we call the <b>GetHelp<\/b> function and exit the script. This section of the script is shown&nbsp;here: <\/p>\n<pre class=\"codeSample\">Function NetworkAdapterFactory($adapter)\n{\n $wmi = Get-WmiObject -Class Win32_NetworkAdapter `\n -filter $filter\n switch ($adapter)\n {\n  \"query\" { $wmi }\n  \"enable\" { $wmi.enable() }\n  \"disable\" { $wmi.disable() }\n  DEFAULT { \n           Write-Host -foregroundcolor red \"unrecognized parameter.\" \n           GetHelp \n           Exit \n          }\n } #end switch\n} #end NetWorkAdapterFactory<\/pre>\n<p>The results of enabling or disabling the network adapter are shown with the default view. You could capture the return code and evaluate it to make a better display. It would more than likely take an additional function. At a minimum you might consider filtering out the system properties. Here the results: <\/p>\n<p><img decoding=\"async\" height=\"375\" alt=\"GetHelp output graphic\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey0922\/enabledisablenicresults.jpg\" width=\"500\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>The entry point of the script evaluates the command line parameters. If the <b>$help<\/b> variable is present, it is only because the script was run with the <b>-help<\/b> parameter. We therefore will call the <b>GetHelp<\/b> function and exit the script. If the <b>$wireless<\/b> variable is present, the script was run with the <b>-wireless<\/b> parameter and we call the <b>getWireless<\/b> function to create the appropriate WMI filter, and pass the value on to the <b>NetworkAdapterFactory<\/b>. We perform a similar procedure if the <b>$wired<\/b> variable is present. Here is the code that decides how the script will perform. The last thing we do is check to see if the <b>$wireless<\/b> or the <b>$wired<\/b> parameters have been specified. If they were not supplied, and we did not specify the <b>-help<\/b> parameter, we call the Help anyway. This will allow the script to display Help when it is run without any command line parameters:<\/p>\n<pre class=\"codeSample\">If($help) { GetHelp ; exit }\nif($wireless) { GetWireless }\nif($wired) { GetWired }\nIf(!$wireless -or !$wired) { GetHelp ; exit }<\/pre>\n<p>TB, this was an awesome question, and there is a lot more we could do to the script to make it easier to use or easier to interpret the results that are returned by running the script. We hope you will see that by the proper use of automation you can ease your frustration with the corporate security guys and reduce your level of stress.<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! At my office I access the corporate network by using a wired ethernet connection. However, when I come home, I have a wireless connection. Since our security guys do not allow us to have multiple network connections active at the same time, I am constantly enabling the wireless connection when I [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,34,332,37,3,45],"class_list":["post-55053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-hardware","tag-network-adapters","tag-networking","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! At my office I access the corporate network by using a wired ethernet connection. However, when I come home, I have a wireless connection. Since our security guys do not allow us to have multiple network connections active at the same time, I am constantly enabling the wireless connection when I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55053","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55053\/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=55053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}