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

Doctor Scripto

Summary: Using Regular Expressions to cleanup string data from NetSh.exe.

Let’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’re going to take that array of information and clean it up so we only have the profile names.

As a quick reminder here’s the line in PowerShell we used to capture the list of Wireless profiles

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

The resulting output looked like this

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 “Netsh.exe wlan delete profile

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’ll look at this with a simple For loop.

$WlanProfileList=foreach ($item in $list) { $item.substring(27) }

If you examine $WlanProfileList you will see this output

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.

We can do that with this regular expression

$list – replace '.*:\s' , ''

What this translates to is

“Get me everything before the next character….. .*”

“That next character is a colon …. :”

“followed by a single space …. \s”

“then replace it with this value (nothing) …. ””

This will produce the same output as the for loop.

Now we’ll use the same process as last time to determine the fastest approach. This time we’ll test it 1000 times.

(1..1000 | foreach-object { Measure-Command { $WlanProfileList=foreach ($item in $list) { $item.substring(27) } }}) | Measure-Object -Property Milliseconds -Average

Vs.

(1..1000 | foreach-object { Measure-Command { $list –replace '.*:\s' , ''}}) | Measure-Object -Property Milliseconds -Average

In this situation you’ll find that due to the low data size the results are too close. But if we’re using Regular Expressions, we can write the entire thing in one line.

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

Pretty cool eh?

Now at this point, we have a list of profile names we can start deleting.

…and for that we’ll meet up next week and discuss how can solve this 😊

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