Is there a way to get only the Ethernet MAC address via command prompt in Windows?

6

3

I use ipconfig /all or getmac /v to get all NIC physical addresses.

But the problem is, generally a computer has more than one NIC card. Also there are some virtual MAC addresses—like Microsoft Virtual Wi-Fi hotspot NIC—which shows only when wifi hotspot is on.

So, how can I collect only the address corresponding to Ethernet via command prompt?

output: (using getmac /fo csv /v and wifi hotspot OFF)

"Connection Name","Network Adapter","Physical Address","Transport Name" "Ethernet","Realtek PCIe FE Family Controller","A0-2B-B8-27-62-12","\Device\Tcpi p_{F1FF9AF6-AD1A-4C5E-8717-C646C9AE466C}" "Wi-Fi","Qualcomm Atheros QCA9565 802.11b/g/n WiFi Adapter","28-E3-47-D2-FB-60", "Media disconnected"

output: (using getmac /fo csv /v and wifi hotspot ON)

"Connection Name","Network Adapter","Physical Address","Transport Name" "Ethernet","Realtek PCIe FE Family Controller","A0-2B-B8-27-62-12","\Device\Tcpi p_{F1FF9AF6-AD1A-4C5E-8717-C646C9AE466C}" "Wi-Fi","Qualcomm Atheros QCA9565 802.11b/g/n WiFi Adapter","28-E3-47-D2-FB-60", "Media disconnected" "Local Area Connection* 7","Microsoft Hosted Network Virtual Adapter","4A-E3-47- D2-FB-60","\Device\Tcpip_{630B2924-03A8-41C1-B1F2-D981A4C263C6}"

List of my all adapters: (wmic nic) http://pastebin.com/zyz9LjJe

UPDATE (possible solution): both @DavidPostill and @wmz 's answer helped me a lot. finally I got a solution to eliminate all virtual adapters with virtual MAC address. x2-xx-xx-xx-xx-xx x6-xx-xx-xx-xx-xx xA-xx-xx-xx-xx-xx xE-xx-xx-xx-xx-xx

MAC addresses with these ranges are used most by virtual MAC adapters. https://serverfault.com/questions/40712/what-range-of-mac-addresses-can-i-safely-use-for-my-virtual-machines

So, after getting all MAC addresses by getmac command, we can filter out those virtual MAC addresses using regular expression. Moreover, we notice that filtering MAC by "wi-fi" is more reliable than filtering by "local area connection". So we filter out connections with "wi-fi" within their names to get final LAN/Ethernet conection.

Sourav Ghosh

Posted 2015-03-22T23:25:11.880

Reputation: 572

Have you seen this answer which uses GNU grep for Windows?

– JakeGould – 2015-03-22T23:38:57.813

1What do you want to do if you have more than one NIC card? – DavidPostill – 2015-03-23T14:16:49.887

@DavidPostill, I need to get ONLY the LAN/Ethernet MAC address, irrespective of how the end user renamed the interface – Sourav Ghosh – 2015-03-24T13:05:09.083

Here is exact answer of your question. (Seems like this question is duplicate) – Just Shadow – 2018-02-15T14:55:55.150

Answers

9

Is there a way to get only the Ethernet MAC address via command prompt?

You can do what you need using a one line (but complicated) sequence of built in commands.

From the command line:

for /f "usebackq tokens=3 delims=," %a in (`getmac /fo csv /v ^| find "Local Area Connection"`) do set MAC=%~a

From a batch file:

for /f "usebackq tokens=3 delims=," %%a in (`getmac /fo csv /v ^| find "Local Area Connection"`) do set MAC=%%~a

How does it work?

We can use:

getmac /fo csv /v

To get the Media Access Control (MAC) address and list of network protocols associated with each address for all local network cards.

Use the /v option to get verbose output (which includes the "Connection Name"). The "Connection Name" is needed later so that we can identify which adapter is the Ethernet connection:

"Connection Name","Network Adapter","Physical Address","Transport Name"
"Local Area Connection","Realtek PCIe GBE Family Controller","F0-BF-97-62-95-5D","\Device\Tcpip_{45B9E87F-83FB-4829-A751-6B62656CC1A8}"
"Wireless Network Connection","Atheros AR9285 Wireless Network Adapter","CC-AF-78-B2-4C-09","\Device\Tcpip_{B108BB0B-CCDC-4ACA-9DFE-5A2F17BC138D}"
"Bluetooth Network Connection","Bluetooth Device (Personal Area Network)","CC-AF-78-B2-4C-0A","Media disconnected"

If we pipe | this output to find we can extract the information for just the Ethernet connection (which has the connection name "Local Area Connection"):

getmac /fo csv /v ^| find "Local Area Connection"

Returns the "Local Area Connection" information in csv (comma delimited) format as follows:

"Local Area Connection","Realtek PCIe GBE Family Controller","F0-BF-97-62-95-5D","\Device\Tcpip_{45B9E87F-83FB-4829-A751-6B62656CC1A8}"

The third (comma delimited) value is the MAC address.

Now we use the for command to extract just the MAC address from the above string.

The string is passed to the for command (by using the ` (backquote) character together with usebackq:

for /f "usebackq tokens=3 delims=," %a in (`string`) do

Returns the 3rd token (value) from the comma delimited string as follows:

"F0-BF-97-62-95-5D"

%~a is used to remove the quotes from the string, leaving:

F0-BF-97-62-95-5D

Finally variable MAC is set to F0-BF-97-62-95-5D:

set MAC=%~a

Note in a batch file every % must be replaced by %%.


Further reeading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • find - Search for a text string in a file & display all the lines where it is found.
  • for /f - Loop command against the results of another command.
  • getmac - Display the Media Access Control (MAC) address and list of network protocols associated with each address for all network cards in each computer, either locally or across a network.
  • parameters - A command line argument (or parameter) is any value passed into a batch script.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.

DavidPostill

Posted 2015-03-22T23:25:11.880

Reputation: 118 938

you might want to elaborate on how one can identify which hex identifier is which interface 'cos looking at this snip http://pastebin.com/raw.php?i=RqD7Uhdu you get mac address and some hex identifier

– barlop – 2015-03-23T09:39:07.427

@barlop That's because your example doesn't use the getmac /v option. I'll update the answer to clarify. Thnaks. – DavidPostill – 2015-03-23T09:44:31.230

1Thank you sir. But in my machine interface name is 'Ethernet'. so I had to use getmac /fo csv /v | find "Ethernet". Is there any way to make it machine independent? So that the code will work on any machine.

also, I need another clarification. why did you use a caret sign before piping the output? getmac /fo csv /v ^| find "Local Area Connection" I think, caret is used to continuation and to escape reserved shell characters. – Sourav Ghosh – 2015-03-23T13:15:16.190

1The carat ^ is required as an escape character when using for with backquotes as | is a reserved character (pipe) – DavidPostill – 2015-03-23T13:24:51.223

Please edit your question to include the output of getmac /fo csv /v for your machine. – DavidPostill – 2015-03-23T13:26:13.087

Did you rename your lan connection from "Local Area Connection" to "Ethernet"? – DavidPostill – 2015-03-23T13:34:17.153

@DavidPostill, I edited the question with output. Actually I don't remember if I edited it to Ethernet or not. Most probably I didn't. The problem is, any user can rename it to anything and I was wondering if there s any general solution. – Sourav Ghosh – 2015-03-24T12:53:37.840

No general solution as far as I know ... :/ – DavidPostill – 2015-03-24T12:56:54.533

Briliant post man! – dExIT – 2017-01-06T11:00:32.693

5

Using wmic

(Ethernet connected interfaces only)

wmic nic where (AdapterTypeId=0 AND netConnectionStatus=2) get MACAddress

More information on available properties: https://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx

Edit: As David noted, this returns WiFi adapter as well (which may or may not be what OP wants). Quick and dirty way to filter anything other than LAN out (based on connection name):

wmic nic where "NetConnectionId like '%Local Area%' and AdapterTypeId=0 AND netConnectionStatus=2" get MACAddress

wmz

Posted 2015-03-22T23:25:11.880

Reputation: 6 132

That command returns both the lan adapter and the wireless adapter MAC addresses on my laptop. – DavidPostill – 2015-03-23T13:28:23.483

@DavidPostill Yes indeed, it seems that for some reason Wireless adapter is reported as 802.3 Ugh... I will try to add some criteria to filter that out – wmz – 2015-03-23T14:15:07.573

Your new command still won't work for the OP who seems to have his renamed his LAN NIC to "Ethernet" (at least according to his comments on my answer). He's looking for a general answer ... :/ – DavidPostill – 2015-03-23T18:07:10.783

@DavidPostill True you may tweak my (or yours) approach to use different properties (eg. look for Ethernet in the adapter name) but it's surprisingly difficult to provide general solution (unless we restrict it to win8 only) – wmz – 2015-03-23T19:41:29.390

@wmz, what if I restrict only to win8? just out of curiosity. – Sourav Ghosh – 2015-03-24T13:02:31.247

@wmz Can you please explain all the columns in wmic nic like AdapterTypeId, DeviceId etc. you can also give any link to KnowledgeBase article – Sourav Ghosh – 2015-03-24T13:41:59.283

1

@SouravGhosh 1. win 8 - you could use Get-NetAdapter (I have not tested it so I'm not 100% sure it will be able to filter out everything you do not want) - see for example this tip: http://www.powershellmagazine.com/2013/04/04/pstip-detecting-wi-fi-adapters/ 2. columns are described in the linked msdn page

– wmz – 2015-03-24T14:35:22.550

Here is an answer which is sustainable to renaming connection names. – Just Shadow – 2018-02-15T14:59:34.270

2

In Powershell you'd do it like this (Windows 8 features the Get-NetAdapter Cmdlet that doesn't ship with Windows 7):

(get-wmiobject win32_networkadapter -Filter "AdapterType LIKE 'Ethernet 802.3'") | select -expand macaddress
11:22:33:45:04:1E

megamorf

Posted 2015-03-22T23:25:11.880

Reputation: 1 494