Summary: Jason Walker explores using Windows PowerShell to get the SSID and password for a wireless network.
Ed Wilson, Microsoft Scripting Guy is here. Today I'm turning over the keyboard to Jason Walker. Jason is an Office 365 deployment consultant, who assists customers in the public sector arena. Here's Jason…
Let me start off with a scenario. You are somewhere, anywhere, and a friend of yours asks you for the password to a wireless network. This could be the wireless network at your house or a hotspot. What do you do? In Windows 7, you could easily get that from a viewable preferred wireless network list. In Windows 8, that was removed, and it has not yet returned.
I decided to tackle this problem. I know from experience that netsh.exe will give me this data, but executables return text. To make this useful in PowerShell, I would have to parse the text to retrieve the wanted data and return a usable object. The first thing that comes to mind is to use the Select-String cmdlet. Let’s dig in…
To get the password for a wireless network, the nestsh.exe syntax is as follows:
netsh.exe wlan show profiles name=’Profile Name’ key=clear
Here is the example output from this command:
The only data I’m concerned with are the lines that contain SSID Name and Key Content. At this point, I’m confident that I could easily get the SSID name and password or key content by running the netsh.exe command and storing the output in a variable. Then I could pass that variable to Select-String once to search for SSID Name, and pass the variable a second time to Select-String to search for Key Content.
I can then parse each search the same way by using the split method and split on the colon ( : ). This will create an array with two elements. I only want the last element, so I specified the last index in the array with a [-1].
As you can see in this example, I now have the SSID. I need to repeat the same process to get the password and then return this data in an object. Here is the complete solution:
This code is a simple example of getting the wireless profile password that will work in WMF 3.0 and newer. In WMF 5.0, the ConvertFrom-String was introduced. This cmdlet has a lot of functionality but the feature that I think is totally awesome is the ability to parse text based off a template file. Here is a post on the Windows PowerShell Blog that explains how ConvertFrom-String works: ConvertFrom-String: Example-based text parsing.
The template file consists of sample output. In the sample output, a template markup defines the structure of the data we want to extract. Here is an example taken from the ConvertFrom-String Help file:
Now let’s apply this to output from netsh.
You can see on line 21 that I defined the name of the wireless profile. On line 31, I defined the password. I am only showing one example of output from the template file, but I have two examples of output in my template file. They give the technology ConvertFrom-String is built on (FlashExtract) and a better idea of the text being parsed.
In the previous example, I put it all together. As you can see, I store the path to my template file in $Template. I run netsh.exe and specify to show the profile information for Test-Wireless. I specify key=clear, and this is piped to ConvertFrom-String and I supply an argument for TemplateFile. The results are a PSObject with the name and password for the wireless profile.
Today, I have demonstrated a simple example of how ConvertFrom-String can be used to extract data from a string of text with very minimal code. I encourage you to read PowerShell – Playing with the new ConvertFrom-String cmdlet by PowerShell MVP Francois-Xavier Cat to see how he uses ConvertFrom-String to parse the output from netstat.exe. Additionally, I showed an example of parsing text with the Select-String cmdlet. I would love to hear how you use PowerShell to parse text. Feel free to leave your ideas in the following Comments box.
Thanks, Jason! Great ideas!
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy