Viewing Wireless Networks with PowerShell Legacy Commands

Combining the netch command in PowerShell will allow users to pull up useful information about local wireless networks.

By Boe Prox

06/22/2017

Just because we are using PowerShell doesn't mean that we don't have times where we must rely on some legacy commands to get the job done. In this case, we are going to have a little fun by using PowerShell with a command that has been available for quite some time and merge the both of these to list out all of the wireless networks in our vicinity.

The command that I will be utilizing today is netsh. If you have worked in the command line to troubleshoot something network related, odds are that you have dipped your toes into the water with this command. Netsh is very powerful to use not only in troubleshooting network related issues, but also for performing a query for information related to a wireless network.

The command that I will be using to list out all of the available wireless networks within range is listed below:

netsh wlan show network mode=bssid

The results provide some great information, but unfortunately it is all listed as a string which means filtering for specific data is very difficult and doesn't promise that you will accurately display all of the information.

This is where PowerShell comes into play. We can skip the first three lines to take out the unnecessary lines and then just focus on the rest of the data. I am also going to trim all of the white space from the beginning and end of each line using Trim() and then cast the entire output as a string so I can more easily parse out the data using regular expressions.

Speaking of regular expressions, we need to build up our expression to grab only the data that we need. In my case, I am looking to grab everything except for the Basic and Other rates. In an effort to make the regular expression a little more readable, I will use a here-string to build out the regular expression.

Now the next step is to split up each Wi-Fi group into its own single string object. This will make it easier to iterate through each network and apply our regular expression to build out an object.

$Networks = $WiFi -split "\r\s+\n"

The last thing left to do is to begin stepping through each network and parse out the data using our expression. I will use an If statement to test each item against the expression and if it is a match, begin building the object out.

In this case, I sorted by signal strength to find out which networks were the closest to my current location. And just like anything that we build, we can make it into a function for easier use!
Function Get-WiFiNetwork {