Get wireless networks

I saw a question on how to get wireless networks which got me thinking about string handling and working with legacy command line utilities. I also wanted to compare the options available in Windows PowerShell and PowerShell Core.

First up is a relatively brute force approach. This approach works is relatively easy to understand and maintain but isn’t very elegant.

function get-wlan {
     [CmdletBinding()]
     param()

    $networks = netsh wlan show networks mode=bssid

    $networks | Select-Object -Skip 4 |
     Where-Object {$_ -ne ”} |
     ForEach-Object {

        if ($psitem.StartsWith(‘SSID’)) {
             $temp = $psitem -split ‘:’
             Write-Verbose -Message $temp[1]
             $network = $($temp[1].Trim())
         }
        
         if ($psitem.TrimStart().StartsWith(‘Authentication’)){
             $temp = $psitem -split ‘:’
             Write-Verbose -Message $temp[1]
             $authentication = $temp[1].Trim()
         }

        if ($psitem.TrimStart().StartsWith(‘Signal’)){
             $temp = $psitem -split ‘:’
             Write-Verbose -Message $temp[1]
             $signal = $temp[1].Trim()
         }

        if ($psitem.TrimStart().StartsWith(‘Radio type’)){
             $temp = $psitem -split ‘:’
            
             $props = [ordered]@{
                 Network = $network
                 Authentication = $authentication
                 Type = $temp[1].Trim()
                 Signal = $signal
             }
             New-Object -TypeName PSobject -Property $props
         }
     }
}

Start by using netsh to get the wireless networks. Skip the first 4 lines (run the netsh command by itself to see what’s being skipped) and filter out blank lines (empty strings). For each line check if it’s of interest and if so split on the : character and take the data into a variable. On the radio type line create an object and output.

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s