8

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.

3
  • 1
    What do you want to do if you have more than one NIC card?
    – DavidPostill
    Mar 23, 2015 at 14:16
  • @DavidPostill, I need to get ONLY the LAN/Ethernet MAC address, irrespective of how the end user renamed the interface Mar 24, 2015 at 13:05
  • Here is exact answer of your question. (Seems like this question is duplicate) Feb 15, 2018 at 14:55

4 Answers 4

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.
9
  • you might want to elaborate on how one can identify which hex identifier is which interface 'cos looking at this snip pastebin.com/raw.php?i=RqD7Uhdu you get mac address and some hex identifier
    – barlop
    Mar 23, 2015 at 9:39
  • @barlop That's because your example doesn't use the getmac /v option. I'll update the answer to clarify. Thnaks.
    – DavidPostill
    Mar 23, 2015 at 9:44
  • 1
    Thank 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. Mar 23, 2015 at 13:15
  • 1
    The carat ^ is required as an escape character when using for with backquotes as | is a reserved character (pipe)
    – DavidPostill
    Mar 23, 2015 at 13:24
  • Please edit your question to include the output of getmac /fo csv /v for your machine.
    – DavidPostill
    Mar 23, 2015 at 13:26
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

8
  • That command returns both the lan adapter and the wireless adapter MAC addresses on my laptop.
    – DavidPostill
    Mar 23, 2015 at 13:28
  • @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
    Mar 23, 2015 at 14:15
  • 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
    Mar 23, 2015 at 18:07
  • @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
    Mar 23, 2015 at 19:41
  • 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: powershellmagazine.com/2013/04/04/… 2. columns are described in the linked msdn page
    – wmz
    Mar 24, 2015 at 14:35
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
1

It seems i have found a bit simpler solution to write mac address in a file

@FOR /F %%i IN ('getmac /fo table /nh') DO ECHO %%i >> D:\mac.txt

will get machine mac address and write it to file mac.txt located at disk D:

Code explanation: With /F key we specify that we pass to cycle @FOR a command 'getmac /fo table /nh' (specified by '') and assign result of command execution to variable i. Then we use ECHO to write variable i value to file. I used

getmac /fo table /nh

to get simpler output than getmac /fo csv This is how output of getmac /fo table /nh looks

00-03-7F-50-5C-0D   \Device\Tcpip_{AD7B1C5C-5027-46A3-B693-95D01D7A0C6C}

So, we only need to pass this string to @FOR cycle which, by default, in this case, will read file before first space (No need to define special keys) and assign read value to variable i. Then we write its value to file with ECHO command

 DO ECHO %%i >> D:\mac.txt

Note that >> will add info to file, while > will overwrite file.

1
  • Oh, man! Thanks!
    – don Rumata
    Apr 13, 2021 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .