{"id":81835,"date":"2017-01-13T00:01:57","date_gmt":"2017-01-13T08:01:57","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=81835"},"modified":"2019-02-18T09:10:14","modified_gmt":"2019-02-18T16:10:14","slug":"view-passwords-of-wireless-profiles-without-using-netsh-exe","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/view-passwords-of-wireless-profiles-without-using-netsh-exe\/","title":{"rendered":"View passwords of wireless profiles without using netsh.exe"},"content":{"rendered":"<p>Ever since the GUI lost the ability to view the password of a wireless profile, I wanted to provide this functionality in PowerShell. A search of the Internet for how to view the password of a Wi-Fi profile returns numerous examples of how to use netsh.exe to accomplish this. I wrote a previous <a target=\"_blank\" href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/23\/get-wireless-network-ssid-and-password-with-powershell\/\">blog post<\/a> about how to view Wi-Fi profiles, and the method I used returned objects, but still relied on netsh.exe. I had some free time, and I wanted to learn about p\/invoke, so I wrote a PowerShell module to do this.<\/p>\n<p>Let\u2019s look at how to do this without netsh. If there isn\u2019t a cmdlet or method in .NET to get what you want, the next step is to look at native (Win32) APIs. As luck would have it, there is a <a target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms706556(v=vs.85).aspx\">Native WiFi API<\/a> that has a <a target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms706274(v=vs.85).aspx\">function for everything you need<\/a>. Let me back up a bit. A native API is unmanaged code. PowerShell is managed code. For managed code to call unmanaged code, you need to leverage Platform Invocation Services or P\/Invoke. This is easily done in PowerShell. All we need is a C# signature that defines a class that renames the C++ types to their C# equivalents and the <code>Add-Type<\/code> cmdlet.<\/p>\n<p>The C# signatures can be obtained by using pinvoke.net as previously explained by <a target=\"_blank\" href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/12\/26\/weekend-scripter-manage-window-placement-by-using-pinvoke\/\">Boe Prox<\/a>. Here is the signature for the <code>WlanGetProfile<\/code> function.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-011317.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-011317.png\" alt=\"Signature for the WlanGetProfile function\" width=\"695\" height=\"203\" class=\"alignnone size-full wp-image-81845\" \/><\/a><\/p>\n<p>I found it very helpful to look at the MSDN documentation for the function that I wanted to use. MSDN explains the parameters of the function and how the function can be used. The MSDN documentation for <a target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms706738(v=vs.85).aspx\">WlanGetProfile<\/a> function is very thorough.<\/p>\n<p>Another note, in my experience some of the signature will work by just copying from pinvoke.net, and others needs some trial-and-error. However, after you understand the concept of a signature and how it\u2019s used, you will be able to adapt it to your scenario. I often found myself searching for the C# equivalents of the C++ types because I would get an error that would say something like \u201ccannot convert DWord to type of Foo\u201d, or something along those lines. After you get a working signature and see your happy new type in the PowerShell console, you will smile.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-011317.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-011317.png\" alt=\"Example of working signature\" width=\"706\" height=\"74\" class=\"alignnone size-full wp-image-81855\" \/><\/a><\/p>\n<p>Here is the <code>WiFiProfileManagement<\/code> module in action. At the time of this blog writing, <code>WifiProfileManagement<\/code> as four cmdlets (<code>Get<\/code>, <code>Set<\/code>, <code>New<\/code>, and <code>Remove-WiFiProfile<\/code>). Let\u2019s take a look at Get-WifiProfile. If I run Get-WiFiProfile without parameters, it will return all the WiFi profiles on my computer.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-011317.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-011317.png\" alt=\"Get-WiFiProfile without parameters\" width=\"581\" height=\"266\" class=\"alignnone size-full wp-image-81865\" \/><\/a><\/p>\n<p>The Password property is blank. If we want to look at the Password, we must use the ClearKey switch.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-011317.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-011317.png\" alt=\"Example of Get-WiFiProfile with the -ClearKey switch\" width=\"621\" height=\"86\" class=\"alignnone size-full wp-image-81875\" \/><\/a><\/p>\n<p>It\u2019s a lot easier than using netsh.exe. You can review the <a target=\"_blank\" href=\"https:\/\/github.com\/jcwalker\/WiFiProfileManagement\">WiFiProfileManagement in it\u2019s entirety on Github<\/a>.<\/p>\n<p><strong>Jason Walker<\/strong>\nSenior Consultant<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever since the GUI lost the ability to view the password of a wireless profile, I wanted to provide this functionality in PowerShell. A search of the Internet for how to view the password of a Wi-Fi profile returns numerous examples of how to use netsh.exe to accomplish this. I wrote a previous blog post [&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":[568,641],"tags":[499,338,377],"class_list":["post-81835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guestblogger","tag-jason-walker","tag-powershell"],"acf":[],"blog_post_summary":"<p>Ever since the GUI lost the ability to view the password of a wireless profile, I wanted to provide this functionality in PowerShell. A search of the Internet for how to view the password of a Wi-Fi profile returns numerous examples of how to use netsh.exe to accomplish this. I wrote a previous blog post [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81835","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=81835"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81835\/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=81835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=81835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=81835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}