{"id":12471,"date":"2011-10-07T00:01:00","date_gmt":"2011-10-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/07\/use-powershell-to-identify-your-real-network-adapter\/"},"modified":"2011-10-07T00:01:00","modified_gmt":"2011-10-07T00:01:00","slug":"use-powershell-to-identify-your-real-network-adapter","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-identify-your-real-network-adapter\/","title":{"rendered":"Use PowerShell to Identify Your Real Network Adapter"},"content":{"rendered":"<p><strong>Summary:<\/strong> Learn how to use Windows PowerShell to identify easily the real network adapter.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>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.<\/p>\n<p>&mdash;BP<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello BP,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. This is one of the problems with technology&mdash;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 <b>ipenabled<\/b><i> <\/i>property set to <b>true<\/b>. An example of a script that uses this technique is in the <a title=\"How Can I Change the IP Address Assigned to a Computer?\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2006\/03\/29\/how-can-i-change-the-ip-address-assigned-to-a-computer.aspx\">How Can I Change the IP Address Assigned to a Computer?<\/a> blog post.<\/p>\n<p>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:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject win32_networkadapterconfiguration -Filter &#8216;ipenabled = &#8220;true&#8221;&#8216;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">DHCPEnabled&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : True<\/p>\n<p style=\"padding-left: 30px\">IPAddress&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {198.134.88.47, fe80::fd99:3ef6:799f:dc0b}<\/p>\n<p style=\"padding-left: 30px\">DefaultIPGateway : {198.134.88.1}<\/p>\n<p style=\"padding-left: 30px\">DNSDomain&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : portseattle.org<\/p>\n<p style=\"padding-left: 30px\">ServiceName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : netw5v64<\/p>\n<p style=\"padding-left: 30px\">Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Intel(R) Wireless WiFi Link 4965AGN<\/p>\n<p style=\"padding-left: 30px\">Index&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 13<\/p>\n<p>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 <b>Win32_networkadapterconfiguration<\/b> class. If I want to enable or disable a network card, I use the <b>win32_networkadapter<\/b> class. The bad thing is that the <b>Win32_Networkadapter<\/b> class does not have an <b>ipenabled<\/b><i> <\/i>property. When I attempt to use such a command, an error is displayed such as the one shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject win32_networkadapter -Filter &#8216;ipenabled = &#8220;true&#8221;&#8216;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject : Invalid query<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:14<\/p>\n<p style=\"padding-left: 30px\">+ Get-WmiObject &lt;&lt;&lt;&lt;&nbsp; win32_networkadapter -Filter &#8216;ipenabled = &#8220;true&#8221;&#8216;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidOperation: (:) [Get-WmiObject], ManagementException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObject Command<\/p>\n<p>The good thing is that both the <b>Win32_networkadapterconfiguration<\/b> class and the <b>win32_NetworkAdapter<\/b> WMI class both share the <b>index<\/b> 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 <b>ipenabled<\/b><i>, <\/i>get the index number of that adapter, and return the network adapter itself:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a = Get-WmiObject win32_networkadapterconfiguration -Filter &#8216;ipenabled = &#8220;true&#8221;&#8216;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a.Index<\/p>\n<p style=\"padding-left: 30px\">13<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject win32_networkadapter -Filter &#8216;index = 13&#8217;<\/p>\n<p style=\"padding-left: 30px\">ServiceName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : netw5v64<\/p>\n<p style=\"padding-left: 30px\">MACAddress&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 00:1F:3B:AD:FF:6D<\/p>\n<p style=\"padding-left: 30px\">AdapterType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ethernet 802.3<\/p>\n<p style=\"padding-left: 30px\">DeviceID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: 13<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Intel(R) Wireless WiFi Link 4965AGN<\/p>\n<p style=\"padding-left: 30px\">NetworkAddresses :<\/p>\n<p style=\"padding-left: 30px\">Speed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 36000000<\/p>\n<p>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 <b>win32_networkadapter<\/b> and <b>win32_networkadapterconfiguration<\/b> WMI classes contain a <b>description<\/b> property. On my laptop, the two outputs are identical:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject win32_networkadapterconfiguration -Filter &#8216;index = 13&#8217; | select description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">description<\/span><\/p>\n<p style=\"padding-left: 30px\">Intel(R) Wireless WiFi Link 4965AGN<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gwmi win32_networkadapter -filter &#8216;index = 13&#8217; | select description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">description<\/span><\/p>\n<p style=\"padding-left: 30px\">Intel(R) Wireless WiFi Link 4965AGN<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>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 <b>Win32_networkadapter<\/b> class, I can create a new name for the <b>netconnectionID<\/b> property. This technique is shown here. <b>Note<\/b>&nbsp; &nbsp;This command requires administrator rights.<\/p>\n<p style=\"padding-left: 30px\">$adapter = Gwmi win32_networkadapter -Filter &#8216;index = 13&#8217;<\/p>\n<p style=\"padding-left: 30px\">$adapter.NetConnectionID = &#8220;ScriptingGuys&#8221;<\/p>\n<p style=\"padding-left: 30px\">$adapter.Put()<\/p>\n<p>The commands and their associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0317.hsg-10-7-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0317.hsg-10-7-11-1.png\" \/><\/a><\/p>\n<p>The following figure shows that the command was successful.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8637.hsg-10-7-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image showing command was successful\" alt=\"Image showing command was successful\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8637.hsg-10-7-11-2.png\" \/><\/a><\/p>\n<p>After I have an easy-to-use network adapter name, I can use it in queries directly:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gwmi win32_networkadapter -Filter &#8216;netconnectionid = &#8220;scriptingguys&#8221;&#8216;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">ServiceName&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;: netw5v64<\/p>\n<p style=\"padding-left: 30px\">MACAddress&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 00:1F:3B:AD:FF:6D<\/p>\n<p style=\"padding-left: 30px\">AdapterType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ethernet 802.3<\/p>\n<p style=\"padding-left: 30px\">DeviceID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 13<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Intel(R) Wireless WiFi Link 4965AGN<\/p>\n<p style=\"padding-left: 30px\">NetworkAddresses :<\/p>\n<p style=\"padding-left: 30px\">Speed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 48000000<\/p>\n<p>I have always made it a practice to name my network adapters. In the past, it required <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2005\/05\/11\/how-can-i-rename-a-local-area-connection.aspx\">a pretty extensive VBScript to accomplish<\/a>, or it required manual intervention (or it required using netsh). Now, a very short Windows PowerShell command accomplishes the task.<\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to identify easily the real network adapter. &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[37,3,4,45,6],"class_list":["post-12471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-networking","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to identify easily the real network adapter. &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12471"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12471\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}