Weekend Scripter: Use PowerShell to Find Auto-Connect Wireless Networks

Weekend Scripter: Use PowerShell to Find Auto-Connect Wireless Networks

  • Comments 5
  • Likes

Summary: Use Windows PowerShell to identify auto-connect wireless networks in Windows 8.

Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about traveling around is that I am constantly confronted with new challenges. Invariably these new challenges tend to cause me to use Windows PowerShell. For example, at TechEd 2013 North America in New Orleans this year, the Expo Hall where I had the Scripting Guys booth contained about 5,000 computers, and at least a hundred wireless networks that were set up by various vendors for their booths.

This, of course, caused my laptop to do the old wireless two-step. It was bouncing from network to network to network. This was in spite of the fact that I had told it to connect to the TechEd wireless network. I started looking around and discovered that the “Manage Wireless Networks” feature in the Network and Sharing Center is apparently removed from Windows 8. Here is a picture of that feature in Windows 7.

Image of menu

Inside there, it was a simple matter of selecting the network and deleting it.

What is the big deal with wireless networks?

Wireless auto-connect is a great feature for a laptop that never leaves trusted premises. So, if my laptop only follows me to the Microsoft office and back to my house, having wireless auto-connect is great. The problem is that I travel in wider circles than that. I may, for example, spend a week in a Marriott Hotel in Seattle, and then spend a few days in a Hyatt in Atlanta, and then slide on into a Hilton in Savannah. While I am at the hotel, I generally connect to the wireless network (here is where the Windows Firewall and network connection profiles come in handy). Therefore, it is irritating to have to type in the code every day I am on the network.

After a while, my collection of auto connect wireless networks becomes rather extensive. Even in Windows 7, I would sometimes forget to groom my wireless connection lists. So at TechEd, there were several networks that my laptop auto connected to because they had common names like Nokia, Linksys, or  AT&T (and yes, there were also some hotel-named networks).

Note  From security and performance perspectives, automatically connecting to a random wireless network is a big deal.

Apparently, I am not the first Windows PowerShell person to write about this. In fact, Lee Holmes just wrote an excellent posting about this the other day: Removing Insecure Wireless Connections with PowerShell. He wrapped Netsh to perform the tasks. For more information about the Netsh commands to manage Windows 8 network connections, see Manage wireless network profiles.

Where are the network auto-connections stored?

I thought the network auto-connection information might be stored in the registry, but I did not see it stored there. So I began looking around, and found them in the C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces folder. Under this folder is a GUID for each network interface. Now, this actually becomes pretty easy to deal with, because I can obtain a list of network interface GUIDS by using the Get-NetAdapter function, as shown here:

PS C:\> (Get-NetAdapter).interfaceGUID

{006DC9B1-2CE6-4F68-A7BC-3885ED63F457}

{EC0A5AB0-734E-41E0-854D-C4CF2D9D0EE2}

{898E0A57-CEFE-4D9B-9E3C-21FCBC3C5118}

{1E9737CA-C7F3-4B34-9C41-EF8DA6E78521}

{12AED542-E967-4AB8-BD94-763AB48195E2}

{6E97AF3F-1DF1-47E1-8049-C148D486FFCE}

So, all I need to do is to figure out which GUID goes to my network adapter. I use the Get-NetAdapter function with no parameters, and obtain the following output:

PS C:\> Get-NetAdapter

 

Name                      InterfaceDescription                    ifIndex Status

----                      --------------------                    ------- ------

Ethernet                  Intel(R) 82579LM Gigabit Network Con...      13 Up

vEthernet (Internal)      Hyper-V Virtual Ethernet Adapter #4          31 Up

vEthernet (ExternalEne... Hyper-V Virtual Ethernet Adapter #3          27 Up

vEthernet (InternalSwi... Hyper-V Virtual Ethernet Adapter #2          23 Up

Wi-Fi                     Intel(R) Centrino(R) Ultimate-N 6300...      12 Not Pre...

Bluetooth Network Conn... Bluetooth Device (Personal Area Netw...      16 Not Pre...

From here, I can see my wireless network adapter is named Wi-Fi. So, I can use this information to obtain my GUID. This is shown here:

PS C:\> (Get-NetAdapter -Name 'wi-fi').interfaceGUID

{12AED542-E967-4AB8-BD94-763AB48195E2}

I will use this code to get me into the appropriate folder when I write my script. The technique is shown here:

$GUID = (Get-NetAdapter -Name 'wi-fi').interfaceGUID

$path = "C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces\$guid"

Inside the wireless network adapter folder is a collection of XML documents.

Each XML document is a wireless connection profile configuration. Inside the document appears all the information I need to manage a connection. This is shown in the following image.

Image of command output

In this image, I marked the properties I want to collect. The script itself is rather simple. I use the Get-ChildItem cmdlet to walk through the wireless network connection folder, I use the Foreach-Object cmdlet to let me work with each individual XML file, and I use the Get-Content cmdlet to read the XML file. I cast the contents to an XMLDOCUMENT type by using the [XML] type accelerator. This portion of the script is presented here:

Get-ChildItem -Path $path -Recurse |

Foreach-Object {

   [xml]$c = Get-Content -path $_.fullname

Next I create a new custom object that picks up the wireless connection name, connection mode, and SSID value as shown here:

New-Object pscustomobject -Property @{

   'name' = $c.WLANProfile.name;

   'mode' = $c.WLANProfile.connectionMode;

   'ssid' = $c.WLANProfile.SSIDConfig.SSID.hex} }

Following is the complete script.

Get-AutomaticWirelessConnections.ps1

 

$GUID = (Get-NetAdapter -Name 'wi-fi').interfaceGUID

$path = "C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces\$guid"

Get-ChildItem -Path $path -Recurse |

Foreach-Object {

   [xml]$c = Get-Content -path $_.fullname

   New-Object pscustomobject -Property @{

   'name' = $c.WLANProfile.name;

   'mode' = $c.WLANProfile.connectionMode;

   'ssid' = $c.WLANProfile.SSIDConfig.SSID.hex} }

When I run the script, the following code appears in the output pane of my Windows PowerShell ISE.

Image of command output

Join me tomorrow when I talk about removing unwanted wireless auto-connect profiles.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • @Ed - excellent.  You just saved me a lot of effort.  Of course you go to have all of the fun but I will use it anyway.

    Now I can clean up the mess that we end up with after months of roaming.

  • PoSh v2 :

    PS>$id=(gwmi win32_networkadapter | ?{$_.name -like "*wireless*"}).guid

    PS>$path = "C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces\$id"

    PS>gci -path $path -r | %{[xml]$con = gc -path $_.fullname; new-object pscustomobject -pro @{'name'= $c.WLANProfile.name; 'mode'=$c.WLANProfile.connectionMode; 'ssid'= $c.WLANProfile.SSIDConfig.SSID.hex}} | ft -auto

  • Sorry, i had $con as the var and changed it to $c for the comment (to match the post)

    PoSh v2 :

    PS>$id=(gwmi win32_networkadapter | ?{$_.name -like "*wireless*"}).guid

    PS>$path = "C:\ProgramData\Microsoft\Wlansvc\Profiles\Interfaces\$id"

    PS>gci -path $path -r | %{[xml]$c = gc -path $_.fullname; new-object pscustomobject -pro @{'name'= $c.WLANProfile.name; 'mode'=$c.WLANProfile.connectionMode; 'ssid'= $c.WLANProfile.SSIDConfig.SSID.hex}} | ft -auto

  • @JRV -- woohoo! Yes, I was really surprised when my laptop started acting squirrelly at TechEd. Glad it helps.

    @Krunch Groovy. Thanks for providing the PowerShell 2.0 example.

  • @Ed - @K

    Yes - I actually updated the code and created a relatively cool CmdLet that does it all on all current versions of Windows.

    You should post your script in the Repository.  I will post mine once I clean it up a little.  - Needs help to finish.

    @K - Even Prince is called Prince now ;>)