Using PowerShell to View and Remove Wireless Profiles in Windows 10 – Part 4

Doctor Scripto

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’re going to finish the task and show you how to delete those Profiles.

As a quick reminder here’s the line in PowerShell we used to capture the list of Wireless profiles by only their names as an Array.

$list=((netsh.exe wlan show profiles) -match '\s{2,}:\s') -replace '.*:\s' , ''

The resulting output containing our Wireless profile names looked like this

From our first posting we saw that to delete a WLAN profile in Netsh.exe we would execute this command.

Netsh.exe wlan delete profile PROFILENAME

Where PROFILENAME was the name of a WLAN profile such as FabrikamHotels.

So we could just purge everything with one loop like this

Foreach ($item in $list)
{
     Netsh.exe wlan delete profile $item
}

But I personally don’t like that. It gives me too little control, I’d rather have a function that says “show me the profiles” or even “show me only this profile”. And then I’d like the Delete to be only the information I give it (Control)

First we’ll build a function to show us the profiles, perhaps even a way to filter on them.

As part of it’s design we’ll build it as if we were building a Cmdlet as we eventually may want to add this to a Module

function Get-WifiProfile
{
     [cmdletbinding()]
     param
     (
     [System.Array]$Name=$NULL
     )

First thing to do is grab the list of WLAN profiles as part of our Begin.  Look closely below and you’ll see the NETSH.EXE line we ran earlier to find the profiles and show only the names.

     Begin
     {
          $list=((netsh.exe wlan show profiles) -match '\s{2,}:\s') -replace '.*:\s' , ''

Now we’ll rebuild that array as a PowerShell Customobject. Currently we’re only returning names, but we could build upon this later and return details of the WLAN Profiles

          $ProfileList=$List | Foreach-object {[pscustomobject]@{Name=$_}}
     }

Now that we have the list, let’s show profiles which match the provided names in the array

     Process
     {
          Foreach ($WLANProfile in $Name)
          {
               $ProfileList | Where-Object { $_.Name -match $WLANProfile }
          }

     }

However if NOTHING is provided, we will return ALL available WLAN Profiles

     End
     {
          If ($Name -eq $NULL)
          {
               $Profilelist
          }
     }
}

Now that it’s built, at least in our Script we could run this for profiles

Get-WifiProfile -name Contoso

If Contoso was there it would return as an object we could consume.

We can even provided a targeted list of WLAN Profiles

Get-WifiProfile -name Contoso, Fabrikam, DoctorScripto

It would of course try to find each one provided, and return the list as an object.

Get-WifiProfile would return everything found.

Now that we have a way to get only Specific profiles or all of them.

Next we’ll build a function in PowerShell to delete defined list of WLAN profiles. In reality we’re just wrapping PowerShell around the NETSH.exe command to simplify its use and automate it a bit more.

We could even trap for “Deleted” or “Not found to delete” conditions.

First we define the name of function. Because we’re planning in the long term to make this into a profile, I used “Get-Verb” to see a list of approved verbs. “Remove” is good to use!

function Remove-WifiProfile
{
     [cmdletbinding()]
     param
     (
          [System.Array]$Name=$NULL
     )
     begin{}

We’ll step the list of provided WLAN Profile names

     process 
     {
          Foreach ($item in $Name)
          {

We will try to purge each individual one

          $Result=(netsh.exe wlan delete profile $item)

As we trap the results, we’ll return something to indicate Success

          If ($Result -match 'deleted')
          {
               "WifiProfile : $Item Deleted"
          }

…or failure (wasn’t there to delete)

          else
          {
               "WifiProfile : $Item NotFound"
          }
     }

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.

But that’s a story for a different day. Meet up with us next week when we look into the Active Directory Cmdlets.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Forum. See you tomorrow. Until then, peace.

Your good friend, Doctor Scripto

PowerShell, Doctor Scripto, Sean Kearney

0 comments

Discussion is closed.

Feedback usabilla icon