{"id":86362,"date":"2019-10-02T01:00:46","date_gmt":"2019-10-02T09:00:46","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=86362"},"modified":"2019-09-30T07:28:05","modified_gmt":"2019-09-30T15:28:05","slug":"using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-2\/","title":{"rendered":"Using PowerShell to View and Remove Wireless Profiles in Windows 10 \u2013 Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Using Measure-Command to determine the fastest approach to a solution.<\/p>\n<p>Last week we were having some fun using PowerShell as a wrapper around the NetSh.exe command&#8217;s output. We were left at a decision point.<\/p>\n<p>Which way to go? A For loop to clean up the data, which worked fine or Regular Expressions. Although both work which was the better path to use?<\/p>\n<p>For me initially, when I first started in PowerShell I very often used a For loop for a few reasons.<\/p>\n<ul>\n<li>It was a simple to navigate loop<\/li>\n<li>I could see the inner workings in a breakpoint<\/li>\n<li>it was pretty easy to troubleshoot problems with variables within<\/li>\n<\/ul>\n<p>But as I progressed in my knowledge of PowerShell I&#8217;ve started to touch on Regular expressions as a solution when possible.<\/p>\n<p>A good reason is although they are more complex to work with, the provide a better response time in some cases. As well they also allow me to obtain data without trying to say to myself &#8220;Hmmm, where does this substring start and end?&#8221;<\/p>\n<p>But right now we have an answer and I&#8217;d like to help you figure out how to decide which would be the better way to proceed. We can use Measure-Command to decide which method is faster.<\/p>\n<p>Here&#8217;s our first example using a loop to skip on the first line. This is something you might do if you&#8217;re first using PowerShell and trying to figure out how to parse data. It is wrapped in the Measure-Command Cmdlet to identify how long it takes<\/p>\n<pre class=\"lang:ps decode:true \">Measure-Command { $list=(netsh.exe wlan show profiles) -match ':'; For ($x=1; $x -lt $list.count; $x++) { $_ } }<\/pre>\n<p>Here is our second solution using only a regular expression.<\/p>\n<pre class=\"lang:ps decode:true \">Measure-Command { $list=(netsh.exe wlan show profiles) -match '\\s{2,}:\\s'; $list }<\/pre>\n<p>Now if you run each in PowerShell you might find sometimes for some reason, the more complex loop takes less time, and then sometimes more.<\/p>\n<p>I had my head scratching on this one. &#8220;This makes no sense&#8221; I was thinking until I realized that with a proper test, I should run multiple samples. Especially since results I saw were roughly less than 1\/3 of second in some cases.<\/p>\n<p>The solution to this was easy, I just ran the same solution 100 times with Measure-command<\/p>\n<pre class=\"lang:ps decode:true \">1..100 | Measure-Command { `\r\n$list=(netsh.exe wlan show profiles) -match ':'; `\r\nFor ($x=1; $x -lt $list.count; $x++) { $_ } `\r\n}<\/pre>\n<p>Now of course this gave me a large result in Milliseconds I would need to divide by 100 to get the results.<\/p>\n<pre class=\"lang:ps decode:true \">(1..100 | Measure-Command { `\r\n$list=(netsh.exe wlan show profiles) -match ':'; `\r\nFor ($x=1; $x -lt $list.count; $x++) { $_ } `\r\n}).totalmilliseconds \/ 100<\/pre>\n<p>&nbsp;<\/p>\n<p>But another way would be to break apart the Measure-Command objects into an array and use the Measure-Object to do the math. For this we&#8217;d need to use For-Each Object to have Measure-Command return each result as a single instance.<\/p>\n<pre class=\"lang:ps decode:true \">(1..100 | foreach-object { Measure-Command { `\r\n$list=(netsh.exe wlan show profiles) -match ':'; `\r\nFor ($x=1; $x -lt $list.count; $x++) { $_ } `\r\n}}) | Measure-Object -Property Milliseconds -Average<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1rem;\">I prefer this second method as it produces a nice report of how many tests, the actual average and the property we&#8217;re measuring. But they both technically work.<\/span><\/p>\n<p>The results I got on average for this code was about 159 milliseconds.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-86366\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-16.png\" width=\"334\" height=\"159\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-16.png 659w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-16-300x142.png 300w\" sizes=\"(max-width: 334px) 100vw, 334px\" \/><\/p>\n<p>I then generated the same solution to test the line using just a regular expression.<\/p>\n<pre class=\"lang:ps decode:true \">(1..100 | Foreach-Object { Measure-Command `\r\n\r\n{ $list=(netsh.exe wlan show profiles) -match '\\s{2,}:\\s'; $list `\r\n\r\n} }) | Measure-Object -Property Milliseconds -Average<\/pre>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"wp-image-86367\" style=\"font-size: 1rem;\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-17.png\" width=\"347\" height=\"164\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-17.png 654w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/09\/word-image-17-300x142.png 300w\" sizes=\"(max-width: 347px) 100vw, 347px\" \/><\/p>\n<p>As you can see with over a 100 runs the Regular Expression solution is SLIGHLTY faster. Which means if this were a very large set of data, it could be a big difference in time.<\/p>\n<p>But with this smaller set of data, a For loop is just as valid solution as long as the data results are consistent.<\/p>\n<p>Going forward in this solution, I&#8217;m going to choose the moderately faster solution (Regular Expressions) because well, I&#8217;m a geek and 2 Milliseconds means a lot to me \ud83d\ude09<\/p>\n<p>Next week we&#8217;ll look deeper into parsing the data to only have the Profile name.<\/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 Measure-Command to determine the fastest approach to a solution. Last week we were having some fun using PowerShell as a wrapper around the NetSh.exe command&#8217;s output. We were left at a decision point. Which way to go? A For loop to clean up the data, which worked fine or Regular Expressions. Although both [&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,763,1738,1,685],"tags":[1740,377,4,154],"class_list":["post-86362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-doctor-scripto","category-performance","category-powershell","category-scripting","category-scripting-techniques","tag-doctor-scripto","tag-powershell","tag-scripting-techniques","tag-sean-kearney"],"acf":[],"blog_post_summary":"<p>Summary: Using Measure-Command to determine the fastest approach to a solution. Last week we were having some fun using PowerShell as a wrapper around the NetSh.exe command&#8217;s output. We were left at a decision point. Which way to go? A For loop to clean up the data, which worked fine or Regular Expressions. Although both [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86362","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=86362"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86362\/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=86362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=86362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=86362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}