Is there a way to get only the Ethernet MAC address via command prompt in Windows?
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.
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.
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
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