Manage Network Adapters with PowerShell

For IT pros, the name of the game is management, and the easier the better. For most of us this means Windows PowerShell. And when we throw Windows Server 2012 into the mix it gets even better. Windows 8 and Windows include a number of new modules specifically designed to manager network adapters, configuration, and security. These modules are based on the new CIM infrastructure that is part of Windows Management Framework 3. However, even if you have PowerShell 3 installed on, say, Windows Server 2008 R2, you don’t get these new toys. The underlying CIM namespaces and classes only exist on Windows 8 and Windows Server 2012. However, if you have a Windows 8 admin desktop and some Windows Server 2012 boxes, you’ll be amazed what you can accomplish.

This is part one of a three-part series. Today I’ll talk about how to get a network adapter with PowerShell and getting network adapter statistics. In part two, I’ll go into addressing and configuration information. Finally, in part three I’ll explore PowerShell cmdlets that might come in handy when troubleshooting a networking issue.

Getting Network Adapters

Enumerating network adapters couldn’t be easier. In PowerShell 3 you don’t even need to import the module first. Simply type the command. Here’s how I can get the network adapters on my laptop:

​ PS C:\> get-netadapter

As you can see in below in Figure 1, I have a lot going on.
Windows 8 Network Adapters
You can narrow down results by name:

​ PS C:\> get-netadapter -Name Ethernet

Or by interface description:

​ PS C:\> get-netadapter -InterfaceDescription *wireless*

Or you can limit your query to physical adapters only.

​ PS C:\> get-netadapter -Physical

My results can be seen in the image below.
Network Adapter Options
You can also query a remote computer via a CIM session, although all you need to do is specify a computer name.

​ PS C:\> get-netadapter -Physical -CimSession NOVO8 | Format-List

As you can see in Figure 3, there’s much more information available.
Querying a remote network adapter

If you think you’ll be working with a remote computer more than once, then it is worth the short amount of time it takes to create a CIMSession. I’m going to jump into my domain and do just that. Here’s the command I’ll run on my Windows 8 client, logged on with an account that has admin rights on the remote computers. (Although New-CimSession supports alternate credentials anyway.)

​ PS C:\> $cim = new-cimsession -comp chi-dc04,chi-fp02,chi-dev01

I can use this collection of CIM Sessions with Get-NetAdapter.

​ PS C:\> get-netadapter -CimSession $cim -physical | Select Name,InterfaceDescription,ifIndex,Status,
MacAddress,LinkSpeed,MTUSize,SystemName | Out-GridView -title "Network Adapters"

The default output from Get-NetAdapter doesn’t include the computer name, and obviously I need to be able to tell what adapter goes with what computer. Since I was at it, I grabbed a few other parameters as well. I piped to Out-Gridview to make it easier to see and work with the results.
Remote Network Adapter Details

Adapter Advanced Properties

We can also discover advanced properties for the network adapter. Here’s a basic command on a system that only has a single adapter.

​ PS C:\> Get-NetAdapterAdvancedProperty

Advanced Adapter Properties
This also works remotely, and you can limit the properties.
Advanced Adapter Properties from remote computers

Getting Network Adapter Statistics

Lastly, let me show you how easy it is to get adapter statistics. First, let’s explore the local adapter.

​ PS C:\> Get-NetAdapterStatistics | format-table -auto
Name       ReceivedBytes ReceivedUnicastPackets SentBytes SentUnicastPackets
----       ------------- ---------------------- --------- ------------------
Ethernet 3      57164647                 136864  56597875             133133

As you can see below in Figure 7, there is even more data available than what I’ve shown above.
It doesn’t take much to convert byte values to MB.

​ PS C:\> Get-NetAdapterStatistics | Select Name,@{Name="ReceivedMB";Expression={$_.ReceivedBytes/1MB}},@{Name="SentMB";Expression={$_.SentBytes/1MB}}
Name                     ReceivedMB                       SentMB
----                     ----------                       ------
Ethernet 3               54.8200120925903                 54.2480316162109

Or query remote machines, as I did in Figure 8.
Remote adapter statistics
It wasn’t intentional, but there was an interruption querying one of the servers. But because I used a CIM session, one of its benefits is resilience. The session retried and eventually reconnected and retrieved the data. Let me leave you with one more example.

​ PS C:\> Get-NetAdapterStatistics -cim $cim | Select PSComputername,Name,*Bytes,*Packets | Out-Gridview -title "Adapter Stats"

Here I have an interactive GUI to analyze network statistics from my servers.
Complete adapter statistics
At no time did I have to resort to any type of scripting. I merely ran cmdlets in an interactive session.
We’ve just started to scratch the surface of what we can accomplish with PowerShell and Windows Server 2012. We’ll continue the exploration in the next article. In the meantime, take a look at full help and examples for all of the cmdlets I used and start trying them out in your own test environment.