{"id":3394,"date":"2013-06-16T00:01:00","date_gmt":"2013-06-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/06\/16\/weekend-scripter-use-powershell-to-manage-auto-connect-wireless-networks\/"},"modified":"2013-06-16T00:01:00","modified_gmt":"2013-06-16T00:01:00","slug":"weekend-scripter-use-powershell-to-manage-auto-connect-wireless-networks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-manage-auto-connect-wireless-networks\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Manage Auto-Connect Wireless Networks"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Use Windows PowerShell to manage auto-connect wireless networks in Windows 8.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I am going to continue my blog about working with auto-connect wireless networks in Windows 8. In yesterday&rsquo;s blog post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/06\/15\/weekend-scripter-use-powershell-to-find-auto-connect-wireless-networks.aspx\" target=\"_blank\">Weekend Scripter: Use PowerShell to Find Auto-Connect Wireless Networks<\/a>, I talked about the process of discovering wireless auto-connect networks.<\/p>\n<p>Today I want to remove all but the specific networks that I desire to keep. There are, for my laptop, two networks I wish to retain: the MrEd wireless network (my home wireless network) and the Nokia Lumia 920 network (the network sharing feature on the Scripting Wife&rsquo;s Windows&nbsp;8 phone that I use to gain wireless Internet access when we are traveling). Everything else should go.&nbsp;<\/p>\n<h2>Checking the files<\/h2>\n<p>The XML files that are used to configure the wireless connection profiles are stored in the ProgramData\\Microsoft\\WlanSvc\\Profiles\\Interfaces folder. Each network adapter identifies itself with a GUID, and there is a folder for each interface. Each file also identifies itself with a GUID. The folder is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4846.hsg-6-16-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4846.hsg-6-16-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>A folder full of GUID-named XML files is not the most friendly thing to attempt to review. This is where Windows PowerShell comes into play because Windows PowerShell makes working with XML very easy.<\/p>\n<h2>Modify yesterday&rsquo;s script<\/h2>\n<p>I decided to modify yesterday&rsquo;s script because it already found the files and produced a nice object with which to work. The first thing I did was create an array of the networks I want to keep. I added this command near the top of the script, as shown here:<\/p>\n<p style=\"padding-left: 30px\">$SafeNetworks = &#8220;Mred&#8221;, &#8220;NOKIA Lumia 920_3303&#8221;<\/p>\n<p>The next thing I did was add a property to the object (the complete path to the XML file) because I will need it when I get ready to delete the files. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">New-Object pscustomobject -Property @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;name&#8217; = $c.WLANProfile.name;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;mode&#8217; = $c.WLANProfile.connectionMode;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;ssid&#8217; = $c.WLANProfile.SSIDConfig.SSID.hex;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;path&#8217; = $_.fullname}<\/p>\n<p>Now I need to determine if the network name is in my <strong>$safeNetworks<\/strong> array. I use the <strong>&ndash;notcontains<\/strong> operator to do this. If the network is not in my <strong>$safeNetworks<\/strong> array, I use the <strong>Remove-Item<\/strong> cmdlet to delete the file. But for testing, I add in the <strong>&ndash;whatif<\/strong> parameter so I can work out any bugs in my code. Here is that portion of the script:<\/p>\n<p style=\"padding-left: 30px\">Foreach-object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; If($SafeNetworks -notcontains $_.Name)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { Remove-Item $_.path -whatif }<\/p>\n<p>And that is basically it. So I give it a whirl to see what happens. Here are the results:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5684.hsg-6-16-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5684.hsg-6-16-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Well, it is time to remove the <strong>&ndash;whatif<\/strong> and give it a go.<\/p>\n<p>Sweet. It looks like it worked. So I open yesterday&rsquo;s script to make sure. It reports only the Nokia and the MrEd network. I look back in the folder, and there are only two XML files there.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2664.hsg-6-16-13-03.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2664.hsg-6-16-13-03.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>OK, one more check&hellip;what does <strong>Netsh<\/strong> say? If you are like me, and you keep your wireless adapter disabled when you are connected to a wired Ethernet connection, it will say that there are no wireless interfaces on the system. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; netsh wlan show profiles<\/p>\n<p>There is no wireless interface on the system.<\/p>\n<p>So I enable my wireless adapter and run the command a second time. This time it reports that I only have two wireless profiles:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; netsh wlan show profiles<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Profiles on interface Wi-Fi:<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Group policy profiles (read only)<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &lt;None&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">User profiles<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; All User Profile&nbsp;&nbsp;&nbsp;&nbsp; : NOKIA Lumia 920_3303<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; All User Profile&nbsp;&nbsp;&nbsp;&nbsp; : MrEd<\/p>\n<p>So it works. Groovy. I think I will created a scheduled task, and run my script on a weekly basis. (For more information about using the Task Scheduler to run a Windows PowerShell script, see my blog post <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/11\/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx\" target=\"_blank\">Weekend Scripter: Use the Windows Task Scheduler to Run a Windows PowerShell Script<\/a>.)<\/p>\n<p>Here is the complete script:<\/p>\n<p><strong>Remove-WirelessAutoConnections.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\">$GUID = (Get-NetAdapter -Name &#8216;wi-fi&#8217;).interfaceGUID<\/p>\n<p style=\"padding-left: 30px\">$path = &#8220;C:\\ProgramData\\Microsoft\\Wlansvc\\Profiles\\Interfaces\\$guid&#8221;<\/p>\n<p style=\"padding-left: 30px\">$SafeNetworks = &#8220;Mred&#8221;, &#8220;NOKIA Lumia 920_3303&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path $path -Recurse |<\/p>\n<p style=\"padding-left: 30px\">Foreach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; [xml]$c = Get-Content -path $_.fullname<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; New-Object pscustomobject -Property @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;name&#8217; = $c.WLANProfile.name;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;mode&#8217; = $c.WLANProfile.connectionMode;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;ssid&#8217; = $c.WLANProfile.SSIDConfig.SSID.hex;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8216;path&#8217; = $_.fullname} |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Foreach-object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; If($SafeNetworks -notcontains $_.Name)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { Remove-Item $_.path } }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; }<\/p>\n<p><span style=\"font-size: 12px\">I invite you to follow me on <\/span><a style=\"font-size: 12px\" href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a><span style=\"font-size: 12px\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a><span style=\"font-size: 12px\">. If you have any questions, send email to me at <\/span><a style=\"font-size: 12px\" href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a><span style=\"font-size: 12px\">, or post your questions on the <\/span><a style=\"font-size: 12px\" href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a><span style=\"font-size: 12px\">. See you tomorrow. Until then, peace.<\/span><\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to manage auto-connect wireless networks in Windows 8. Microsoft Scripting Guy, Ed Wilson, is here. Today I am going to continue my blog about working with auto-connect wireless networks in Windows 8. In yesterday&rsquo;s blog post, Weekend Scripter: Use PowerShell to Find Auto-Connect Wireless Networks, I talked about the process of [&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":[36,37,3,63,61,45],"class_list":["post-3394","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-security","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to manage auto-connect wireless networks in Windows 8. Microsoft Scripting Guy, Ed Wilson, is here. Today I am going to continue my blog about working with auto-connect wireless networks in Windows 8. In yesterday&rsquo;s blog post, Weekend Scripter: Use PowerShell to Find Auto-Connect Wireless Networks, I talked about the process of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3394","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=3394"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3394\/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=3394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}