Use PowerShell to Identify Your Real Network Adapter

Use PowerShell to Identify Your Real Network Adapter

  • Comments 15
  • Likes

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

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>You should connect the Win32_NetworkAdapterConfiguration to the associated Win32_NetworkAdaptor like this:</p> <p>$NetAdapterConfig=gwmi win32_networkadapterconfiguration -filter &#39;ipenabled=&quot;true&quot;&#39; </p> <p># (there may be more than one of these, so you might need to select one of them... &nbsp;then...)</p> <p># Get the associated NetAdpater</p> <p>$NetAdapterConfig.GetRelated(&#39;win32_networkadapter&#39;)</p> <p>HTH,</p> <p>Chris Warwick</p> <p>@cjwarwickps</p>

  • <p>@Chris</p> <p>You can hjsut PosH it:</p> <p>gwmi win32_networkadapterconfiguration -filter &#39;ipenabled=&quot;true&quot;&#39; </p> <p> &nbsp; &nbsp; %{$_.GetRelated(&#39;win32_networkadapter&#39;)}</p> <p>Get them all with no errors.</p>

  • <p>@jv that is a cool technique. </p>

  • <p>@Ed.</p> <p>To be more clear. I just started to learn those underlying bits about WMI. &nbsp;I have been probing under the covers lately. &nbsp;I can&#39;t remember which blog triggered my interest in that. If I remember I will post the link here,</p> <p>The WMF team seems to be working on new things for WMI. &nbsp;I suspect the surprises will be even better than those little ditties they put into PosH V2 WMI (WMF 2.0).</p> <p>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. &nbsp;The future looks to be much brighter.</p> <p>Those two decorations (Related/Relationships) are muxh moreintuitive that CIM semantics.</p> <p>Happy tinkering….</p>

  • <p>@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. </p>

  • <p>@Ed</p> <p>I don&#39;t find WMI all that inconsistent until you get into Vendor additions like HP and Dell. &nbsp;Then it gets real screwy, &nbsp;MOst of the bas CIM and Win32/MSFT classes are fairly easy to understand. &nbsp;It is an older management model but the WMF Team looks to be serious about rethinking its use and usability.</p> <p>Maybe 3.0 will bring some relief.</p>

  • <p>@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 :-)</p>

  • <p>Great article and lots of wisdom here.</p>

  • <p>Based on your post, I spent a lot of time figuring this out:</p> <p>$ids = Get-WmiObject -Class Win32_NetworkAdapter -computer SOME_REMOTE_COMPUTER | where-object {$_.netenabled -eq &quot;true&quot; -OR $_.netenabled -eq &quot;false&quot;} | select -expandproperty index</p> <p>ForEach ($id in $ids)</p> <p>{</p> <p>Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computer SOME_REMOTE_COMPUTER -filter &quot;index = $id&quot; | ft Description,IPAddress,IPEnabled,Index,MACAddress -autosize</p> <p>}</p> <p>Then I took a closer look at the Win32_NetworkAdapter class here:</p> <p><a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/aa394216%28VS.85%29.aspx">msdn.microsoft.com/.../aa394216%28VS.85%29.aspx</a></p> <p>I discovered that you can do it, your script is just using the wrong name. &nbsp;Use &quot;netenabled&quot; intead of &quot;ipenabled&quot;.</p> <p>This is much simpler than the above.</p> <p>Get-WmiObject -Class Win32_NetworkAdapter -computer SOME_REMOTE_COMPUTER | where-object {$_.netenabled -eq &quot;true&quot; -OR $_.netenabled -eq &quot;false&quot;} | ft Description,NetEnabled,Index,MACAddress -autosize</p>

  • <p>I wish I could take some of that back. &nbsp;This part doesn&#39;t work right:</p> <p>Where {$_.IpEnabled -eq &#39;False&#39;}</p> <p>It still only returns interfaces where IpEnabled is &#39;True&#39;. &nbsp;Oh well. &nbsp;So, it looks like the first version still works best.</p> <p>Happy scripting.</p>

  • <p>Get-WmiObject -Class Win32_NetworkAdapter &nbsp;| where {$_.netenabled -eq &#39;False&#39;} | ft index,netenabled</p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;netenabled</p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;----- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;----------</p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True</p> <p> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True</p>

  • <p>@ken &nbsp;Read the definitions in the link. NetEnabled is not anything like IPenabled.</p> <p>IPenabled telss us the it is a configured TCP-IP adapter. &nbsp;NetEnabled only tells us that the adapter (any kind of adapter) is enabled.</p> <p>This logic you use is meaningless. &nbsp;Think about it.</p> <p>{$_.netenabled -eq &quot;true&quot; -OR $_.netenabled -eq &quot;false&quot;} </p> <p>There are only two possibilities. &nbsp;Including both is identical to including none.</p>

  • <p>#KEn</p> <p>yOu have your true and false all bollixed up.</p> <p>Try thgis to see what I mean.</p> <p>Get-WmiObject -Class Win32_NetworkAdapter &nbsp;| where {$_.netenabled -ne $false}</p> <p>Notice that in PowerShell we use $true and $false. These are booleans and NOT strings.</p>

  • 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

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