Removing Insecure Wireless Connections with PowerShell

Troy Hunt recently posted a great discussion about the dangers of letting your devices automatically connect to insecure wireless networks – especially if those have a common name like ‘Starbucks’ or ‘Apple Demo’.

All devices let you "forget" a network when you are in range, and Windows 7 (and before) even had an interface that let you delete networks when they were out of range.

This dialog was removed in Windows 8, although the ‘netsh’ command still lets you accomplish it from the command line.

To fill this gap, a bunch of tools have sprung up, and Scott Hanselman just threw his hat into the ring, too. He wrote a custom C# app ("WiFi.exe"), even mentioning "I could have used PowerShell or something…".

If you wanted to go the PowerShell route, what would that look like?

Quite simple, in fact.

After a few helper functions, it’s just "Get-UnsecureWlanProfile | Remove-WlanProfile".

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
function Get-WlanProfile 
{ 
   
netsh wlan show all |
 
       
sls "^[\s]+Name\s+:" -Context 16 |
 
       
%
 { 
           
$conn = [Ordered]
 @{} 
           
$_.Line, $_.Context.PostContext[1], $_.Context.PostContext[15] | %
 { 
               
$label,$value = $_ -split ‘:’
 
               
$conn[$label.Trim()] = $value.
Trim() 
            } 

            $result = [PSCustomObject] $conn 
           
if($result.Name -and $result.Authentication) { $result
 } 
        } 
} 

function Get-UnsecureWlanProfile 
{ 
   
Get-WlanProfile | ?
 { 
        (
$_.Authentication -eq ‘Open’) -and
 
        (
$_."Connection mode" -match "automatically"
) 
    } 
} 

function Remove-WlanProfile 
{ 
   
param
( 
       
[Parameter(ValueFromPipelineByPropertyName)]
 
       
$Name
 
    ) 

    netsh wlan delete profile name="$Name" 
}

3 Responses to “Removing Insecure Wireless Connections with PowerShell”

  1. Scott writes:

    Epic, you’re my hero, again.

    (Only works on English Windows, though. ;) )

  2. How to delete Open or Insecure Wi-Fi HotSpots from Windows 8: Wifi.exe Command Line Utility with Source - Scott Hanselman writes:

    […] Lee Holmes went and wrote it in PowerShell! […]

  3. Yasutaka Ito writes:

    Very cool and informative. Why don’t you add this on Github and get user contributions from people like Scott himself.

Leave a Reply