{"id":86419,"date":"2019-10-09T02:00:52","date_gmt":"2019-10-09T10:00:52","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=86419"},"modified":"2019-10-08T21:18:48","modified_gmt":"2019-10-09T05:18:48","slug":"using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-3\/","title":{"rendered":"Using PowerShell to View and Remove Wireless Profiles in Windows 10 \u2013 Part 3"},"content":{"rendered":"<p><strong>Summary<\/strong>: Using Regular Expressions to cleanup string data from NetSh.exe.<\/p>\n<p>Let&#8217;s remember the last two discussions. The first time we looked at using PowerShell to identify wireless profiles with some simple regular expressions. We followed up the next week with how to identify which approach would be the fastest.<\/p>\n<p>Today we&#8217;re going to take that array of information and clean it up so we only have the profile names.<\/p>\n<p>As a quick reminder here&#8217;s the line in PowerShell we used to capture the list of Wireless profiles<\/p>\n<pre class=\"lang:ps decode:true\">$list=(netsh.exe wlan show profiles) -match '\\s{2,}:\\s'<\/pre>\n<p>The resulting output looked like this<\/p>\n<p><img decoding=\"async\" width=\"814\" height=\"175\" class=\"wp-image-86420\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/word-image-4.png\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/word-image-4.png 814w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/word-image-4-300x64.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/word-image-4-768x165.png 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/p>\n<p>What we need to do now is cleanup this data so show only the Wireless Profile name so that we can pass that name back to &#8220;<strong>Netsh.exe wlan delete profile<\/strong>&#8221;<\/p>\n<p>Again we can look at two solutions. I can solve this with a Regular Expression but again, depending on how well versed you are in PowerShell we&#8217;ll look at this with a simple For loop.<\/p>\n<p>$WlanProfileList=foreach ($item in $list) { $item.substring(27) }<\/p>\n<p>If you examine $WlanProfileList you will see this output<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-medium wp-image-86422\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/Output-300x125.png\" alt=\"\" width=\"300\" height=\"125\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/Output-300x125.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/10\/Output.png 357w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>But another solution would be to use a Regular Expression. What I would REALLY like to do is find everything up to the Colon and the space preceding the name and erase that.<\/p>\n<p>We can do that with this regular expression<\/p>\n<pre class=\"lang:ps decode:true\">$list \u2013 replace '.*:\\s' , ''<\/pre>\n<p>What this translates to is<\/p>\n<p><strong><em>\u201cGet me everything before the next character\u2026.. .*\u201d <\/em><\/strong><\/p>\n<p><strong><em>\u201cThat next character is a colon \u2026. :\u201d<\/em><\/strong><\/p>\n<p><strong><em>\u201cfollowed by a single space \u2026. \\s\u201d<\/em><\/strong><\/p>\n<p><strong><em>\u201cthen replace it with this value (nothing) \u2026. &#8221;\u201d<\/em><\/strong><\/p>\n<p>This will produce the same output as the for loop.<\/p>\n<p>Now we&#8217;ll use the same process as last time to determine the fastest approach. This time we&#8217;ll test it 1000 times.<\/p>\n<pre class=\"lang:ps decode:true \">(1..1000 | foreach-object { Measure-Command { $WlanProfileList=foreach ($item in $list) { $item.substring(27) } }}) | Measure-Object -Property Milliseconds -Average<\/pre>\n<p>Vs.<\/p>\n<pre class=\"lang:ps decode:true \">(1..1000 | foreach-object { Measure-Command { $list \u2013replace '.*:\\s' , ''}}) | Measure-Object -Property Milliseconds -Average<\/pre>\n<p>In this situation you&#8217;ll find that due to the low data size the results are too close. But if we&#8217;re using Regular Expressions, we can write the entire thing in one line.<\/p>\n<pre class=\"lang:ps decode:true \">$list=((netsh.exe wlan show profiles) -match '\\s{2,}:\\s') -replace '.*:\\s' , ''<\/pre>\n<p>Pretty cool eh?<\/p>\n<p>Now at this point, we have a list of profile names we can start deleting.<\/p>\n<p>\u2026and for that we&#8217;ll meet up next week and discuss how can solve this \ud83d\ude0a<\/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<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Using Regular Expressions to cleanup string data from NetSh.exe. Let&#8217;s remember the last two discussions. The first time we looked at using PowerShell to identify wireless profiles with some simple regular expressions. We followed up the next week with how to identify which approach would be the fastest. Today we&#8217;re going to take that [&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,2189,1738,687,685],"tags":[1740,377,174,4,154],"class_list":["post-86419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-doctor-scripto","category-networking","category-powershell","category-regular-expressions","category-scripting-techniques","tag-doctor-scripto","tag-powershell","tag-regular-expressions","tag-scripting-techniques","tag-sean-kearney"],"acf":[],"blog_post_summary":"<p>Summary: Using Regular Expressions to cleanup string data from NetSh.exe. Let&#8217;s remember the last two discussions. The first time we looked at using PowerShell to identify wireless profiles with some simple regular expressions. We followed up the next week with how to identify which approach would be the fastest. Today we&#8217;re going to take that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86419","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=86419"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86419\/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=86419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=86419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=86419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}