Summary: Microsoft Scripting Guy, Ed Wilson, talks about messing around with wireless settings with Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. OK, I will admit it (in case you did not know), I am a geek. I also love my job at Microsoft. I get to play around with Windows PowerShell all day. I have meetings with people who are working on Windows PowerShell, and I attend user groups for Windows PowerShell, and speak at conferences to people who love Windows PowerShell. So what is not to love? Nothing! It is great!
One of the real treats I have is being on our internal Windows PowerShell discussion alias. Of course, some of the entries on the alias are confidential, but most are not. The writers are a bunch of geeks like me, who love experimenting and pushing the limits of Windows PowerShell. In the early days of Windows PowerShell 1.0 (when it was in beta), I believe someone wrote a song called there must be 50 ways to kill a process. Because Windows PowerShell is so powerful, and because it can leverage so many different technologies, there are quite often many different ways of accomplishing the same task. Which one is best?
The standard consultant answer may very well apply, but it depends. In all likelihood your Windows PowerShell experience is completely different than mine. I do stuff because it is fun and because I want to learn new things. You are probably working with Windows PowerShell because you have a task to perform, and you are in search of a solution. In that case, the best solution is the one that is easiest, fastest, most powerful, or that uses a technology with which you are already familiar. As you can see, best is certainly relative.
A few weeks ago I wrote a couple of blog posts that talked about enumerating wireless network connection profiles and cleaning up wireless profiles that are not needed. Also a while back, there was an email on our internal Windows PowerShell discussion alias where one of our engineers mentioned the Windows.Networking.Connectivity.NetworkInformation .NET Framework class. He showed how to use it to get the wireless connection cost and the network cost type. “Hmmmm,” I thought…
Use the NetworkInformation class to get connection cost info
You can find information on MSDN about the NetworkInformation class. But in reality, the class methods are pretty straight forward, and they are discoverable via Get-Member. My static members are shown here:
PS C:\> [Windows.Networking.Connectivity.NetworkInformation]::GetConnectionProfiles()
NetworkAdapter NetworkSecuritySettings ProfileName
-------------- ----------------------- -----------
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Bluetooth Network Connection
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... MrEd
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... NOKIA Lumia 920_3303
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Hyatt_Wifi
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... A-MSFTWLAN
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... KFC Wifi
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Hyatt
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Free_WiFi
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... STARBUCKS
The trick to using this class in Windows PowerShell does not really show up in the class documentation. When calling the class, I need to specify that the Windows.ContentType is equal to WindowsRunTime. This loads the class, and permits me to easily call its methods. Following is where I do that (I cast it to void because I do not need any feedback from loading the assembly):
[void][Windows.Networking.Connectivity.NetworkInformation,Windows,ContentType=WindowsRuntime]
Now, I call the GetInternetConnectionProfile method, and I store the returned ConnectionProfile object in the $connectionProfile variable. I now call the GetConnectionCost method. This method returns a ConnectionCost object. From that, I get the NetworkCostType property. Here is the remainder of the script:
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
$connectionCost = $connectionProfile.GetConnectionCost()
$networkCostType = $connectionCost.NetworkCostType
echo $networkCostType
When I run the script, the following output displays and tells me that my Internet connection is unrestricted:
Exploring security settings or network adapter information
The properties from the ConnectionProfile object each return additional objects. This is shown here:
PS C:\> [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile() | Get-Member -MemberType Property
TypeName: Windows.Networking.Connectivity.ConnectionProfile
Name MemberType Definition
---- ---------- ----------
NetworkAdapter Property Windows.Networking.Connectivity.NetworkAdapter NetworkAdapter {get;}
NetworkSecuritySettings Property Windows.Networking.Connectivity.NetworkSecuritySettings NetworkSecuritySettings {get;}
ProfileName Property string ProfileName {get;}
I can directly access each of the objects by using dotted notation after the method call. For example, I can get the NetworkAdapter information by placing NetworkAdapter at the end of the method call as shown here:
[Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().NetworkAdapter
In the image that follows, I explore the NetworkSecuritySettings object and the NetworkAdapter object.
Another way to find stored network connection profiles
OK, that is cool. But what I want to do is to look at stored network connection profiles. This is simple—tab expansion even works for this in the ISE. I call the GetConnectionProfiles static method. This technique is shown here:
PS C:\> [Windows.Networking.Connectivity.NetworkInformation]::GetConnectionProfiles()
NetworkAdapter NetworkSecuritySettings ProfileName
-------------- ----------------------- -----------
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Bluetooth Network Connection
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... MrEd
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... NOKIA Lumia 920_3303
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Hyatt_Wifi
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... A-MSFTWLAN
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... KFC Wifi
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Hyatt
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... Free_WiFi
Windows.Networking.Connectivity.Network... Windows.Networking.Connectivity.Network... STARBUCKS
That is all there is to using Windows PowerShell to mess around with wireless settings. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
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