{"id":74661,"date":"2015-11-23T00:01:00","date_gmt":"2015-11-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/23\/get-wireless-network-ssid-and-password-with-powershell\/"},"modified":"2019-02-18T09:20:59","modified_gmt":"2019-02-18T16:20:59","slug":"get-wireless-network-ssid-and-password-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/get-wireless-network-ssid-and-password-with-powershell\/","title":{"rendered":"Get Wireless Network SSID and Password with PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Jason Walker explores using Windows PowerShell to get the SSID and password for a wireless network.<\/p>\n<p>Ed Wilson, Microsoft Scripting Guy is here. Today I&#039;m turning over the keyboard to Jason Walker. Jason is an Office 365 deployment consultant, who assists customers in the public sector arena. Here&#039;s Jason&#8230;<\/p>\n<p>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.<\/p>\n<p>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 <b>Select-String<\/b> cmdlet. Let&rsquo;s dig in&#8230;<\/p>\n<p>To get the password for a wireless network, the nestsh.exe syntax is as follows:<\/p>\n<p style=\"margin-left:30px\">netsh.exe wlan show profiles name=&rsquo;Profile Name&rsquo; key=clear<\/p>\n<p>Here is the example output from this command:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5383.1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5383.1.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The only data I&rsquo;m concerned with are the lines that contain <b>SSID Name<\/b> and <b>Key Content<\/b>. At this point, I&rsquo;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 <b>Select-String<\/b> once to search for <b>SSID Name<\/b>, and pass the variable a second time to <b>Select-String<\/b> to search <b>for Key Content<\/b>.&nbsp;<\/p>\n<p>I can then parse each search the same way by using the split method and split on the colon ( <b>: <\/b>). 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 <b>[-1]<\/b>.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2045.2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2045.2.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4062.3.PNG\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4062.3.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>This code is a simple example of getting the wireless profile password that will work in WMF&nbsp;3.0 and newer. In WMF&nbsp;5.0, the <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/dn807178.aspx\">ConvertFrom-String<\/a> 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 <b>ConvertFrom-String<\/b> works: <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2014\/10\/31\/convertfrom-string-example-based-text-parsing.aspx\">ConvertFrom-String: Example-based text parsing<\/a>.<\/p>\n<p>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 <b>ConvertFrom-String<\/b> Help file:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/58143.4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/58143.4.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now let&rsquo;s apply this to output from netsh.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1376.5.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1376.5.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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 <b>ConvertFrom-String<\/b> is built on (<a href=\"http:\/\/research.microsoft.com\/en-us\/um\/people\/sumitg\/pubs\/pldi14-flashextract.pdf\" target=\"_blank\">FlashExtract<\/a>) and a better idea of the text being parsed.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2630.6.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2630.6.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In the previous example, I put it all together. As you can see, I store the path to my template file in <b>$Template<\/b>. I run netsh.exe and specify to show the profile information for <b>Test-Wireless<\/b>. I specify <b>key=clear<\/b>, and this is piped to <b>ConvertFrom-String<\/b> and I supply an argument for <b>TemplateFile<\/b>. The results are a PSObject with the name and password for the wireless profile.<\/p>\n<p>Today, I have demonstrated a simple example of how <b>ConvertFrom-String<\/b> can be used to extract data from a string of text with very minimal code. I encourage you to read <a href=\"http:\/\/www.lazywinadmin.com\/2014\/09\/powershell-playing-with-new-convertfrom.html\" target=\"_blank\">PowerShell &#8211; Playing with the new ConvertFrom-String cmdlet<\/a> by PowerShell MVP Francois-Xavier Cat to see how he uses <b>ConvertFrom-String<\/b> to parse the output from netstat.exe. Additionally, I showed an example of parsing text with the <b>Select-String<\/b> cmdlet. I would love to hear how you use PowerShell to parse text. Feel free to leave your ideas in the following <b>Comments<\/b> box.<\/p>\n<p>~Jason<\/p>\n<p>Thanks, Jason! Great ideas!<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#039;m turning over the keyboard to Jason Walker. Jason is an Office 365 deployment consultant, who assists customers in the public sector arena. Here&#039;s Jason&#8230; Let me start off with [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,338,3,45],"class_list":["post-74661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-jason-walker","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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&#039;m turning over the keyboard to Jason Walker. Jason is an Office 365 deployment consultant, who assists customers in the public sector arena. Here&#039;s Jason&#8230; Let me start off with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74661"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74661\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}