Use PowerShell to Identify Your Real Network Adapter

Doctor Scripto

Summary: Learn how to use Windows PowerShell to identify easily the real network adapter.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I have a problem that perhaps you can assist with. I know about WMI, and I know there is a class that represents a network adapter. The problem is that when I run the command, I get back lots of stuff that is not a real network adapter. I am sure I am not the first person to see this problem, so I want to know how other people solve this problem.

—BP

 

Hey, Scripting Guy! AnswerHello BP,

Microsoft Scripting Guy Ed Wilson here. This is one of the problems with technology—it is always changing. In the old days, it was relatively simple to use WMI to work with network adapters. All one needed to do was to choose the adapter that had the ipenabled property set to true. An example of a script that uses this technique is in the How Can I Change the IP Address Assigned to a Computer? blog post.

On my laptop today, that approach still works. The command to find a network adapter that is IP enabled is shown here along with the associated output:

PS C:\> Get-WmiObject win32_networkadapterconfiguration -Filter ‘ipenabled = “true”‘

 

DHCPEnabled      : True

IPAddress        : {198.134.88.47, fe80::fd99:3ef6:799f:dc0b}

DefaultIPGateway : {198.134.88.1}

DNSDomain        : portseattle.org

ServiceName      : netw5v64

Description      : Intel(R) Wireless WiFi Link 4965AGN

Index            : 13

Cool. However, there is a problem. This command retrieves the configuration of the network adapter; it does not get the network adapter itself. For example, if I want to change the IP address or the DNS server address, I use the Win32_networkadapterconfiguration class. If I want to enable or disable a network card, I use the win32_networkadapter class. The bad thing is that the Win32_Networkadapter class does not have an ipenabled property. When I attempt to use such a command, an error is displayed such as the one shown here:

PS C:\> Get-WmiObject win32_networkadapter -Filter ‘ipenabled = “true”‘

 

Get-WmiObject : Invalid query

At line:1 char:14

+ Get-WmiObject <<<<  win32_networkadapter -Filter ‘ipenabled = “true”‘

    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException

    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObject Command

The good thing is that both the Win32_networkadapterconfiguration class and the win32_NetworkAdapter WMI class both share the index property. This means I can use the result of one query to feed into a query for the other class. In the following example, I find the network adapter that is ipenabled, get the index number of that adapter, and return the network adapter itself:

PS C:\> $a = Get-WmiObject win32_networkadapterconfiguration -Filter ‘ipenabled = “true”‘

PS C:\> $a.Index

13

PS C:\> Get-WmiObject win32_networkadapter -Filter ‘index = 13’

ServiceName      : netw5v64

MACAddress       : 00:1F:3B:AD:FF:6D

AdapterType      : Ethernet 802.3

DeviceID         : 13

Name             : Intel(R) Wireless WiFi Link 4965AGN

NetworkAddresses :

Speed            : 36000000

Another way to find a specific network adapter is to look at the maker of the adapter or the description of the adapter. For example, on my laptop the same company makes both the wired Ethernet connection and the wireless network adapter. You cannot rely upon using only the network adapter that is connected, because more than one network adapter could be connected at the same time. Fortunately, both win32_networkadapter and win32_networkadapterconfiguration WMI classes contain a description property. On my laptop, the two outputs are identical:

PS C:\> Get-WmiObject win32_networkadapterconfiguration -Filter ‘index = 13’ | select description

 

description

Intel(R) Wireless WiFi Link 4965AGN

 

PS C:\> gwmi win32_networkadapter -filter ‘index = 13’ | select description

 

description

Intel(R) Wireless WiFi Link 4965AGN

 

One of the tricks I use so that I can always find the correct network adapter is assign specific names to the network adapters. Using WMI and the Win32_networkadapter class, I can create a new name for the netconnectionID property. This technique is shown here. Note   This command requires administrator rights.

$adapter = Gwmi win32_networkadapter -Filter ‘index = 13’

$adapter.NetConnectionID = “ScriptingGuys”

$adapter.Put()

The commands and their associated output are shown in the following figure.

Image of commands and associated output

The following figure shows that the command was successful.

Image showing command was successful

After I have an easy-to-use network adapter name, I can use it in queries directly:

PS C:\> gwmi win32_networkadapter -Filter ‘netconnectionid = “scriptingguys”‘

 

ServiceName      : netw5v64

MACAddress       : 00:1F:3B:AD:FF:6D

AdapterType      : Ethernet 802.3

DeviceID         : 13

Name             : Intel(R) Wireless WiFi Link 4965AGN

NetworkAddresses :

Speed            : 48000000

I have always made it a practice to name my network adapters. In the past, it required a pretty extensive VBScript to accomplish, or it required manual intervention (or it required using netsh). Now, a very short Windows PowerShell command accomplishes the task.

Well, BP, that is all there is to working with the network adapter, index names, and NetConnectionIDs. I invite you to join me tomorrow for more Windows PowerShell cool tricks.

 

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

 

 

0 comments

Discussion is closed.

Feedback usabilla icon