Prof. Powershell

Nosing Around Network Adapters in PowerShell

The Get-WMIObject cmdlet will have you sniffing out what's on your network in no time. Here's how.

When teaching PowerShell or helping out in a scripting forum, I often come across people looking to identify network adapter information in PowerShell, often using WMI. The first thing to realize is that the network adapter is separate from the configuration. Thus, the first step is identifying the primary adapter. This is not an easy task given that Windows often adds a lot of virtual network adapters as well, not to mention adapters installed by virtualization software like VMware.

The best technique I've come up with is to query the Win32_NetworkAdapter class for adapters that have an Ethernet type and a speed value greater than 0:

PS C:\> get-wmiobject win32_networkadapter -filter "adaptertype like '%ethernet%' AND Speed>0"

ServiceName : L1C
MACAddress  : 00:26:9E:C8:09:72
AdapterType : Ethernet 802.3
DeviceID    : 9
Name        : Atheros AR8131 PCI-E Gigabit Ethernet Controller (NDIS 6.20)
NetworkAddresses :
Speed       : 1000000000

Another approach which also seems to work is this command:

PS C:\> get-wmiobject win32_networkadapter -filter "netenabled='true'"

In the testing I've done, both approaches seems to consistently return the primary network adapter, but I make no guarantees, especially if you have a true multi-homed system. Although that just means when we get to the configuration you'll have to process each adapter.

If you know the adapter's DeviceID, then you can get the corresponding network adapter configuration. In my example, the NIC has a deviceid of 9 which corresponds to the index property of the Win32_NetworkAdapterConfiguration class:

PS C:\> get-wmiobject win32_networkadapterconfiguration -filter "index=9"

DHCPEnabled      : True
IPAddress        : {172.16.10.124, fe80::2dcb:7bd4:54e:e6bf}
DefaultIPGateway : {172.16.10.254}
DNSDomain        : jdhitsolutions.local
ServiceName      : L1C
Description      : Atheros AR8131 PCI-E Gigabit Ethernet
                   Controller (NDIS 6.20)
Index            : 9

Depending on your environment you might also be able to directly get the configuration by filtering where the IPEnabled property is true.

PS C:\> get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled='True'"

DHCPEnabled      : True
IPAddress        : {172.16.10.124, fe80::2dcb:7bd4:54e:e6bf}
DefaultIPGateway : {172.16.10.254}
DNSDomain        : jdhitsolutions.local
ServiceName      : L1C
Description      : Atheros AR8131 PCI-E Gigabit Ethernet
                   Controller (NDIS 6.20)
Index            : 9

One thing to be aware of is that some of the properties are arrays, such as the IP address and DefaultIPGateway:

PS C:\> get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled='True'" |
>> Select MACAddress,IPAddress,DefaultIPGateway,DNSHostname,DNSServerSearchOrder
>>

MACAddress           : 00:26:9E:C8:09:72
IPAddress            : {172.16.10.124, fe80::2dcb:7bd4:54e:e6bf}
DefaultIPGateway     : {172.16.10.254}
DNSHostname          : SERENITY
DNSServerSearchOrder : {172.16.10.1}

You can tell these are arrays because the values are in braces {}. The challenge is that this doesn't export very well to a CSV file. Assuming that the first value is the primary one you wish to capture, you could try something like this:

PS C:\> get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled='True'" |
>> Select MACAddress,@{Name="IP";Expression={$_.IPAddress[0]}},
>> @{Name="DefaultGateway";Expression={$_.DefaultIPGateway[0]}},
>> DNSHostname,@{Name="DNSServer";Expression={$_.DNSServerSearchOrder[0]}} |
>> Export-CSV c:\work\ipdata.csv

It wouldn't take too much work to expand this to a group of computers or to include other information like the DHCP server.

There are .NET classes you can also use to access network adapter information, but they require a bit more work than using a cmdlet like Get-WMIObject. Perhaps I'll cover them in a future column; it's all up to you to speak up if this this interests you.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular