{"id":86351,"date":"2019-09-25T01:00:09","date_gmt":"2019-09-25T09:00:09","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=86351"},"modified":"2019-09-25T03:20:59","modified_gmt":"2019-09-25T11:20:59","slug":"using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-1\/","title":{"rendered":"Using PowerShell to View and Remove Wireless Profiles in Windows 10 \u2013 Part 1"},"content":{"rendered":"<p><strong>Summary<\/strong>: Using PowerShell and Regular Expressions as a wrapper on NetSh.exe output to identify the profiles.<\/p>\n<p>Hey, Doctor Scripto!<\/p>\n<p>I have a whole pile of old Wireless profiles that have been collecting dust. I know I can use NETSH to view and delete them, but the process is very manual. Do you know if there is an easier way to clear them up?<\/p>\n<p>\u2014WF<\/p>\n<p>Hello WF, you asked I shall help. Doctor Scripto is in the house!<\/p>\n<p>I know the process you mean, I had to do this a few years ago on one of my old laptops. I had wireless profiles from my home, hotels, offices, coffee shops. It was just silly. I think there were about twenty-five or more at the time.<\/p>\n<p>For those unfamiliar with the process in the question, you can run the following command in a shell to list your wireless profiles.<\/p>\n<p><strong>Netsh wlan show profile<\/strong><\/p>\n<p>You will get an output similar to this (Depending on how many profiles it could be a much larger or smaller list)<\/p>\n<p><img decoding=\"async\" class=\"wp-image-86352\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-12.png\" width=\"612\" height=\"294\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-12.png 828w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-12-300x144.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-12-768x369.png 768w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/p>\n<p>Let&#8217;s imagine we need to delete the profile called &#8216;ScriptoHouse&#8217;. We would then execute this Command in the shell.<\/p>\n<p><strong>Netsh wlan delete profile ScriptoHouse<\/strong><\/p>\n<p>That would remove the profile for the Hotspot called &#8216;ScriptoHouse&#8217;. For one profile, this is not too bad.<\/p>\n<p>But what if you have MANY profiles? Or you&#8217;d like a way to show these an object to manipulate? PowerShell can aid in the aspect.<\/p>\n<p>Our first challenge is to capture the output as an object in PowerShell<\/p>\n<pre class=\"lang:ps decode:true \">$list=(netsh.exe wlan show profiles)<\/pre>\n<p>Once you have this as a String object you can play with regular expressions.<\/p>\n<p>One thing you&#8217;ll notice is all of the lines with Profile names on them have a colon and preceded by &#8216;All user Profile&#8217; and about 5 spaces.<\/p>\n<p>So first let&#8217;s use a simple Regular Expression that shows us &#8220;all lines with a :&#8221;<\/p>\n<pre class=\"lang:ps decode:true \">$list -match ':'<\/pre>\n<p><img decoding=\"async\" class=\"wp-image-86353\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-13.png\" width=\"647\" height=\"139\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-13.png 814w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-13-300x64.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-13-768x165.png 768w\" sizes=\"(max-width: 647px) 100vw, 647px\" \/><\/p>\n<p>Now that&#8217;s cleaned up a lot of the data to a point we have a smaller array!<\/p>\n<p>Since the first line is something we don&#8217;t need, we could at this point probably just say &#8220;skip the first line and show the remaining data\u2026&#8221;.<\/p>\n<p>This would actually work well with a small loop like this.<\/p>\n<pre class=\"lang:ps decode:true\">$list=(netsh.exe wlan show profiles) -match ':'\r\n\r\n# We start at position 1 in the Array to skip over the first line\r\n\r\nFor ($x=1; $x -lt $li.count; $x++)\r\n{\r\n# Examining the String shows the Profile name starts at position 27\r\n$li[$x]\r\n}<\/pre>\n<p><span style=\"font-size: 1rem;\">At this point we could call it good for pulling up the profile information with PowerShell other than some additional cleanup. But if a regular expression got us THIS far, could it get us a bit further? The answer is &#8220;Yes&#8221;.<\/span><\/p>\n<p>Instead of doing a loop, we&#8217;ll build a bigger regular expression. We can expand the expression to say &#8220;Find me only lines with at least two spaces followed by a Colon and then one space&#8221;.<\/p>\n<p>We can do that with the following -match statement<\/p>\n<pre class=\"lang:ps decode:true\">$list=(netsh.exe wlan show profiles) -match '\\s{2,}:\\s'<\/pre>\n<p>If you&#8217;re new to regular expressions the line above translates to<\/p>\n<p><em><strong>&#8220;Find me a section with two spaces or more at least \u2026.. \\s{2,}&#8221;<\/strong><\/em><\/p>\n<p><em><strong>&#8220;It&#8217;s followed by a colon \u2026. :&#8221;<\/strong><\/em><\/p>\n<p><em><strong>&#8220;Then it needs to be another space \u2026. \\s&#8221;<\/strong><\/em><\/p>\n<p>Now we&#8217;ve found two ways of solving the same problem. Why choose one over the other? Well as I often say to myself, &#8220;There are a dozen ways to do the same thing in PowerShell and they&#8217;re all correct&#8221;<\/p>\n<p>Sometimes it&#8217;s a matter of deciding which you are most comfortable with, and of course sometimes, which method is most efficient.<\/p>\n<p>We&#8217;ll look at that next week when we continue our work on using PowerShell to work with Wireless profiles.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Your good friend, Doctor Scripto<\/strong><\/p>\n<p>PowerShell, Doctor Scripto, Sean Kearney, Regular Expression<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Using PowerShell and Regular Expressions as a wrapper on NetSh.exe output to identify the profiles. Hey, Doctor Scripto! I have a whole pile of old Wireless profiles that have been collecting dust. I know I can use NETSH to view and delete them, but the process is very manual. Do you know if 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":[1739,1738,686,687,685,678],"tags":[1740,377,174,154],"class_list":["post-86351","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-doctor-scripto","category-powershell","category-regex","category-regular-expressions","category-scripting-techniques","category-text","tag-doctor-scripto","tag-powershell","tag-regular-expressions","tag-sean-kearney"],"acf":[],"blog_post_summary":"<p>Summary: Using PowerShell and Regular Expressions as a wrapper on NetSh.exe output to identify the profiles. Hey, Doctor Scripto! I have a whole pile of old Wireless profiles that have been collecting dust. I know I can use NETSH to view and delete them, but the process is very manual. Do you know if there [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86351","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=86351"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86351\/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=86351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=86351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=86351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}