Using PowerShell To View and Remove Wireless Profiles in Windows 10 - Part 4 - Scripting Blog (Archived)
Using PowerShell To View and Remove Wireless Profiles in Windows 10 - Part 4 - Scripting Blog (Archived)
10 – Part 4
Doctor Scripto
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.
Copy
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.
Copy
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
Copy
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.
Copy
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
Copy
Now that we have the list, let’s show profiles which match the provided names in the array
Copy
Process
{
Foreach ($WLANProfile in $Name)
{
$ProfileList | Where-Object { $_.Name -match $WLANProfile }
}
Copy
End
{
If ($Name -eq $NULL)
{
$Profilelist
}
}
}
Now that it’s built, at least in our Script we could run this for profiles
It would of course try to find each one provided, and return the list as an object.
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.
Copy
function Remove-WifiProfile
{
[cmdletbinding()]
param
(
[System.Array]$Name=$NULL
)
begin{}
Copy
process
{
Foreach ($item in $Name)
{
Copy
Copy
Copy
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 [email protected],
or post your questions on the Official Scripting Forum. See you tomorrow. Until then, peace.