This post includes a Get-WLAN function to show information about wireless LAN connections, including the SSID and signal strength. It also demonstrates creating a PowerShell wrapper for a built-in Windows command.

Why create a PowerShell exe wrapper

PowerShell Tools are re-usable functions that can be used stand-alone or in a pipeline

Sometimes its more convenient to create a wrapper script using the output of a command line tool than try to create the function entirely in PowerShell. The example below creates a PowerShell command to get information about WI-Fi connections on the local computer.

Using Regex to parse text output

Regular expressions are the ideal way to convert text output from a command line tool into PowerShell objects, making a re-usable pipeline tool. Regex is very powerful, but also intimidating. The solution below uses a handy shortcut to identify boundaries in the output - the not operator - ^.
For example, [^:]+ means match one or more characters that are not a colon. In the example below, this is used to split the text on each line in the command output.

The netsh output below needs to be split into key value pairs (e.g. SSID = MyWifi) and converted to a PSObject. For each line of output, the colon character is the obvious boundary between the key name and the value.

Native Command Output

C:\> netsh wlan show interfaces

There is 1 interface on the system:

Name                   : Wi-Fi
Description            : Intel(r) Dual Band Wireless-AC 8260
GUID                   : 42bce393-237c-4bd4-9d5e-18020ba8bb87
Physical address       : b7:8a:60:a5:f7:d8
State                  : connected
SSID                   : MyWiFi
BSSID                  : 30:d4:2e:50:de:7f
Network type           : Infrastructure
Radio type             : 802.11n
Authentication         : WPA2-Personal
Cipher                 : CCMP
Connection mode        : Profile
Channel                : 6
Receive rate (Mbps)    : 115.6
Transmit rate (Mbps)   : 115.6
Signal                 : 97%
Profile                : MyWiFi

Hosted network status  : Not available

The regex explained

The PowerShell snippet below shows the regular expression and how the matches are added to a hash table collection as name = value.

$Properties = @{}
$result = netsh wlan show interfaces

$Result | ForEach-Object {

    if ($_ -match '^\s+(?<name>[^:]+):\s(?<value>.*)$') {

        $name = $Matches['name'].Trim()
        $val = $Matches['value'].Trim()

        $Properties.Add($name, $val)
    }

}

The Foreach-Object loop above processes the NetSH command output line-by-line.
Each line (the $_ variable) is tested for a match against the RegEx expression using the PowerShell -match operator.

The “not” operator [^:]+ captures all the characters until the colon and saves them in the named capture group “name” ?<name>. The match then expects a colon followed by a space. Finally, everything until the end of the line is saved to the named capture group “value” ?<value>.

  • MATCHES(0) = the entire line
  • MATCHES(’name’) = from the start of the line, match any character that is not a colon
  • MATCHES(‘value’) = match everything from colon [space] to the end of the line

RegEx

A complete Get-WLAN PowerShell function is provided below.

PowerShell Script

For some practice with Regular Expressions, check out RegEx Golf or Regex Crosswords. There is even a Regular Expressions day.



This article was originally posted on Write-Verbose.com