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

Doctor Scripto

Dr Scripto

Summary: Using PowerShell and Regular Expressions as a wrapper on NetSh.exe output to identify the profiles.

Hey, Doctor Scripto!

I have a whole pile of old Wireless profiles that have been collecting dust. I know I can use NETSH to view and delete them, but the process is very manual. Do you know if there is an easier way to clear them up?

—WF

Hello WF, you asked I shall help. Doctor Scripto is in the house!

I know the process you mean, I had to do this a few years ago on one of my old laptops. I had wireless profiles from my home, hotels, offices, coffee shops. It was just silly. I think there were about twenty-five or more at the time.

For those unfamiliar with the process in the question, you can run the following command in a shell to list your wireless profiles.

Netsh wlan show profile

You will get an output similar to this (Depending on how many profiles it could be a much larger or smaller list)

Let’s imagine we need to delete the profile called ‘ScriptoHouse’. We would then execute this Command in the shell.

Netsh wlan delete profile ScriptoHouse

That would remove the profile for the Hotspot called ‘ScriptoHouse’. For one profile, this is not too bad.

But what if you have MANY profiles? Or you’d like a way to show these an object to manipulate? PowerShell can aid in the aspect.

Our first challenge is to capture the output as an object in PowerShell

Once you have this as a String object you can play with regular expressions.

One thing you’ll notice is all of the lines with Profile names on them have a colon and preceded by ‘All user Profile’ and about 5 spaces.

So first let’s use a simple Regular Expression that shows us “all lines with a :”

Now that’s cleaned up a lot of the data to a point we have a smaller array!

Since the first line is something we don’t need, we could at this point probably just say “skip the first line and show the remaining data…”.

This would actually work well with a small loop like this.

At this point we could call it good for pulling up the profile information with PowerShell other than some additional cleanup. But if a regular expression got us THIS far, could it get us a bit further? The answer is “Yes”.

Instead of doing a loop, we’ll build a bigger regular expression. We can expand the expression to say “Find me only lines with at least two spaces followed by a Colon and then one space”.

We can do that with the following -match statement

If you’re new to regular expressions the line above translates to

“Find me a section with two spaces or more at least ….. \s{2,}”

“It’s followed by a colon …. :”

“Then it needs to be another space …. \s”

Now we’ve found two ways of solving the same problem. Why choose one over the other? Well as I often say to myself, “There are a dozen ways to do the same thing in PowerShell and they’re all correct”

Sometimes it’s a matter of deciding which you are most comfortable with, and of course sometimes, which method is most efficient.

We’ll look at that next week when we continue our work on using PowerShell to work with Wireless profiles.

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, Regular Expression

 

Doctor Scripto
Dr Scripto

Scripter, PowerShell, vbScript, BAT, CMD

Follow Dr Scripto   

0 comments

    Leave a comment