{"id":86525,"date":"2019-10-16T01:00:59","date_gmt":"2019-10-16T09:00:59","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=86525"},"modified":"2019-10-16T02:49:05","modified_gmt":"2019-10-16T10:49:05","slug":"using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-view-and-remove-wireless-profiles-in-windows-10-part-4\/","title":{"rendered":"Using PowerShell to View and Remove Wireless Profiles in Windows 10 \u2013 Part 4"},"content":{"rendered":"<p><strong>Summary<\/strong>: Using Windows PowerShell to purge Wlan profiles with NetSh.exe.<\/p>\n<p>Last week we had a way with a Regular Expression to run one Netsh.exe command and show only the profile names as a PowerShell Array.<\/p>\n<p>Today we&#8217;re going to finish the task and show you how to delete those Profiles.<\/p>\n<p>As a quick reminder here&#8217;s the line in PowerShell we used to capture the list of Wireless profiles by only their names as an Array.<\/p>\n<pre class=\"lang:ps decode:true\">$list=((netsh.exe wlan show profiles) -match '\\s{2,}:\\s') -replace '.*:\\s' , ''<\/pre>\n<p>The resulting output containing our Wireless profile names looked like this<\/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>From our first posting we saw that to delete a WLAN profile in Netsh.exe we would execute this command.<\/p>\n<p><strong>Netsh.exe wlan delete profile <em>PROFILENAME<\/em><\/strong><\/p>\n<p>Where <strong><em>PROFILENAME<\/em><\/strong> was the name of a WLAN profile such as <strong>FabrikamHotels<\/strong>.<\/p>\n<p>So we could just purge everything with one loop like this<\/p>\n<pre class=\"lang:ps decode:true \">Foreach ($item in $list)\r\n{\r\n\u00a0 \u00a0\u00a0 Netsh.exe wlan delete profile $item\r\n}<\/pre>\n<p>But I personally don\u2019t like that. It gives me too little control, I\u2019d rather have a function that says \u201cshow me the profiles\u201d or even \u201cshow me only this profile\u201d. And then I\u2019d like the Delete to be only the information I give it (Control)<\/p>\n<p>First we\u2019ll build a function to show us the profiles, perhaps even a way to filter on them.<\/p>\n<p>As part of it\u2019s design we\u2019ll build it as if we were building a Cmdlet as we eventually may want to add this to a Module<\/p>\n<pre class=\"lang:ps decode:true\">function Get-WifiProfile\r\n{\r\n     [cmdletbinding()]\r\n     param\r\n     (\r\n     [System.Array]$Name=$NULL\r\n     )<\/pre>\n<p>First thing to do is grab the list of WLAN profiles as part of our Begin.\u00a0 Look closely below and you&#8217;ll see the NETSH.EXE line we ran earlier to find the profiles and show only the names.<\/p>\n<pre class=\"lang:ps decode:true\">     Begin\r\n     {\r\n          $list=((netsh.exe wlan show profiles) -match '\\s{2,}:\\s') -replace '.*:\\s' , ''<\/pre>\n<p>Now we\u2019ll rebuild that array as a PowerShell Customobject. Currently we\u2019re only returning names, but we could build upon this later and return details of the WLAN Profiles<\/p>\n<pre class=\"lang:ps decode:true\">          $ProfileList=$List | Foreach-object {[pscustomobject]@{Name=$_}}\r\n     }<\/pre>\n<p>Now that we have the list, let\u2019s show profiles which match the provided names in the array<\/p>\n<pre class=\"lang:ps decode:true\">     Process\r\n     {\r\n          Foreach ($WLANProfile in $Name)\r\n          {\r\n               $ProfileList | Where-Object { $_.Name -match $WLANProfile }\r\n          }\r\n\r\n     }<\/pre>\n<p>However if NOTHING is provided, we will return ALL available WLAN Profiles<\/p>\n<pre class=\"lang:ps decode:true\">     End\r\n     {\r\n          If ($Name -eq $NULL)\r\n          {\r\n               $Profilelist\r\n          }\r\n     }\r\n}<\/pre>\n<p>Now that it\u2019s built, at least in our Script we could run this for profiles<\/p>\n<p><strong>Get-WifiProfile -name Contoso<\/strong><\/p>\n<p>If Contoso was there it would return as an object we could consume.<\/p>\n<p>We can even provided a targeted list of WLAN Profiles<\/p>\n<p><strong>Get-WifiProfile -name Contoso, Fabrikam, DoctorScripto<\/strong><\/p>\n<p>It would of course try to find each one provided, and return the list as an object.<\/p>\n<p>Get-WifiProfile would return everything found.<\/p>\n<p>Now that we have a way to get only Specific profiles or all of them.<\/p>\n<p>Next we\u2019ll build a function in PowerShell to delete defined list of WLAN profiles. In reality we\u2019re just wrapping PowerShell around the NETSH.exe command to simplify its use and automate it a bit more.<\/p>\n<p>We could even trap for \u201cDeleted\u201d or \u201cNot found to delete\u201d conditions.<\/p>\n<p>First we define the name of function. Because we\u2019re planning in the long term to make this into a profile, I used \u201cGet-Verb\u201d to see a list of approved verbs. \u201cRemove\u201d is good to use!<\/p>\n<pre class=\"lang:default decode:true\">function Remove-WifiProfile\r\n{\r\n     [cmdletbinding()]\r\n     param\r\n     (\r\n          [System.Array]$Name=$NULL\r\n     )\r\n     begin{}\r\n<\/pre>\n<p>We\u2019ll step the list of provided WLAN Profile names<\/p>\n<pre class=\"lang:default decode:true \">     process \r\n     {\r\n          Foreach ($item in $Name)\r\n          {<\/pre>\n<p>We will try to purge each individual one<\/p>\n<pre class=\"lang:ps decode:true\">          $Result=(netsh.exe wlan delete profile $item)<\/pre>\n<p>As we trap the results, we\u2019ll return something to indicate Success<\/p>\n<pre class=\"lang:default decode:true \">          If ($Result -match 'deleted')\r\n          {\r\n               \"WifiProfile : $Item Deleted\"\r\n          }<\/pre>\n<p>\u2026or failure (wasn\u2019t there to delete)<\/p>\n<pre class=\"lang:ps decode:true \">          else\r\n          {\r\n               \"WifiProfile : $Item NotFound\"\r\n          }\r\n     }<\/pre>\n<p>Now we have two useful functions we can use in a script to deal with Profiles. We could even package these into a module or extend their features to allow proper piping.<\/p>\n<p>But that\u2019s a story for a different day. Meet up with us next week when we look into the Active Directory Cmdlets.<\/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","protected":false},"excerpt":{"rendered":"<p>Summary: Using Windows PowerShell to purge Wlan profiles with NetSh.exe. Last week we had a way with a Regular Expression to run one Netsh.exe command and show only the profile names as a PowerShell Array. Today we&#8217;re going to finish the task and show you how to delete those Profiles. As a quick reminder here&#8217;s [&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,687,685,688],"tags":[1740,69,377,154],"class_list":["post-86525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-doctor-scripto","category-powershell","category-regular-expressions","category-scripting-techniques","category-text-manipulation","tag-doctor-scripto","tag-functions","tag-powershell","tag-sean-kearney"],"acf":[],"blog_post_summary":"<p>Summary: Using Windows PowerShell to purge Wlan profiles with NetSh.exe. Last week we had a way with a Regular Expression to run one Netsh.exe command and show only the profile names as a PowerShell Array. Today we&#8217;re going to finish the task and show you how to delete those Profiles. As a quick reminder here&#8217;s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86525","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=86525"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/86525\/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=86525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=86525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=86525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}