Use PowerShell to Identify Your Real Network Adapter

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

Hey, 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

Hello 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 ipenabledproperty 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:

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 ipenabledproperty. When I attempt to use such a command, an error is displayed such as the one shown here:

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:

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:

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.

The following figure shows that the command was successful.

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

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.

# (there may be more than one of these, so you might need to select one of them... then...)

# Get the associated NetAdpater

$NetAdapterConfig.GetRelated('win32_networkadapter')

HTH,

Chris Warwick

@cjwarwickps

JV

7 Oct 2011 10:46 PM

@Chris

You can hjsut PosH it:

gwmi win32_networkadapterconfiguration -filter 'ipenabled="true"'

%{$_.GetRelated('win32_networkadapter')}

Get them all with no errors.

ed wilson

9 Oct 2011 4:41 PM

@jv that is a cool technique.

JV

10 Oct 2011 6:43 AM

@Ed.

To be more clear. I just started to learn those underlying bits about WMI. I have been probing under the covers lately. I can't remember which blog triggered my interest in that. If I remember I will post the link here,

The WMF team seems to be working on new things for WMI. I suspect the surprises will be even better than those little ditties they put into PosH V2 WMI (WMF 2.0).

If WMI can reach the operational and logical consistency of PowerShell we will have a wonderful tool. Currently WMI is a bit ambiguous and somewhat difficult to understand. The future looks to be much brighter.

Those two decorations (Related/Relationships) are muxh moreintuitive that CIM semantics.

Happy tinkering….

Ed Wilson

10 Oct 2011 8:32 PM

@JV WMI is fun to play with ... but it really is inconsistent. For some things, it is the ONLY way to manage them. You are correct, PowerShell 2.0 brought some nice new features.

JV

10 Oct 2011 8:48 PM

@Ed

I don't find WMI all that inconsistent until you get into Vendor additions like HP and Dell. Then it gets real screwy, MOst of the bas CIM and Win32/MSFT classes are fairly easy to understand. It is an older management model but the WMF Team looks to be serious about rethinking its use and usability.

Maybe 3.0 will bring some relief.

Ed Wilson

10 Oct 2011 9:02 PM

@JV I agree with the basics, win32_bios, win32_logicaldick, win32_processor etc. are all pretty standard. It is when you move from the WBEMCORE classes that things get exciting :-)

Notice that in PowerShell we use $true and $false. These are booleans and NOT strings.

Anonymous

30 Dec 2013 11:31 AM

How would you execute these commands for a remote machine on the network? I want to do this, but I want to display all machines on our domain with a certain network card installed. All our users are on AD.
Sorry, I'm new at this.
Thanks

Arun

24 Mar 2014 1:30 PM

How to get the Device ID, using only the Name : Intel(R) Wireless WiFi Link 4965AGN?????????????