Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get MAC address from command line / batch file in Vista?

7,509 views
Skip to first unread message

Dave R. at dot

unread,
Feb 27, 2008, 1:08:27 PM2/27/08
to
In a batch file that uses only standard Windows commands (no third-party
utilities) I need to be able to extract the MAC address of the ethernet
adapter installed in the machines we deploy and display it to the user
in a format like "The MAC Address is: 00-00-00-00-00-00". I'm running
Vista Business Edition with SP1, and I've gotten close with the
following (which worked under XP SP2):

ipconfig /all|find "Physical Address">c:\windows\temp\macaddress.txt
for /f "tokens=2 delims=:" %%i in (c:\windows\temp\macaddress.txt) do
@echo The MAC Address is %%i

The problem is that under Vista, I end up with three MAC addresses
displayed because there are multiple Physical Addresses listed in
IPCONFIG's output (listed below).

How can I limit the display to the Ethernet adapter's MAC address?


IPCONFIG /ALL

Windows IP Configuration

Host Name . . . . . . . . . . . . : XXXXXX
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : mycompany.com

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . : mycompany.com
Description . . . . . . . . . . . : Intel(R) 82566DM-2 Gigabit
Network Connection
Physical Address. . . . . . . . . : 00-1A-A0-C3-3C-5D
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . :
fe80::f03c:d8f1:d28b:befc%8(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.0.111(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, January 21, 1872 8:57:34
AM
Lease Expires . . . . . . . . . . : Sunday, March 02, 2008 12:25:49
PM
Default Gateway . . . . . . . . . : 192.168.0.1
DHCP Server . . . . . . . . . . . : 192.168.0.1
DNS Servers . . . . . . . . . . . : 192.168.0.1
192.168.0.2
Primary WINS Server . . . . . . . : 192.168.0.1
Secondary WINS Server . . . . . . : 192.168.0.2
NetBIOS over Tcpip. . . . . . . . : Enabled

Tunnel adapter Local Area Connection* 6:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : mycompany.com
Description . . . . . . . . . . . : isatap.mycompany.com
Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes

Tunnel adapter Local Area Connection* 7:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
Physical Address. . . . . . . . . : 02-00-54-55-4E-01
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes

Regards,

Dave


Harlan Grove

unread,
Feb 27, 2008, 6:21:24 PM2/27/08
to
"Dave R." <dwragle (at) drbsystems (dot) com> wrote...
...
> . . . I need to be able to extract the MAC address of the ethernet

>adapter installed in the machines we deploy and display it to the
>user in a format like "The MAC Address is: 00-00-00-00-00-00". . . .
...

>ipconfig /all|find "Physical Address">c:\windows\temp\macaddress.txt
...

>The problem is that under Vista, I end up with three MAC addresses
>displayed because there are multiple Physical Addresses listed in
>IPCONFIG's output (listed below).
>
>How can I limit the display to the Ethernet adapter's MAC address?
...

With something like the following.


@REM locate MAC ID in ipconfig/all output
@setlocal enableextensions
@echo off

set f=%0.tmp

ipconfig/all | findstr ^
/C:"Ethernet adapter Local Area Connection:" ^
/C:"Physical Address" ^
> %f%

for /F "delims=" %%a in (%f%) do call :PROC "%%a"
echo The MAC Address is: %MACA%
goto :EOF

:PROC
set a=%~1
set a=%a: =%
set a=%a:.=%

if "%a:~0,8%" == "Ethernet" (
set s=next line will have MACA
) else if defined s (
set MACA=%a:PhysicalAddress:=%
set s=
)


Note: findstr.exe is a standard utility included with Windows XP. If
you have find.exe, you should also have findstr.exe.

Harlan Grove

unread,
Feb 27, 2008, 6:24:57 PM2/27/08
to
Harlan Grove <hrln...@gmail.com> wrote...
...

>echo The MAC Address is: %MACA%
>goto :EOF
...

Forgot to add the statement

del %f%

between the two statements above.

Kam-Hung Soh

unread,
Feb 27, 2008, 8:13:19 PM2/27/08
to

Here's a one-liner using "getmac" instead of "ipconfig":

for /f "tokens=3 delims=," %a in ('"getmac /v /fo csv | findstr
Ethernet"') do echo %a

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
a>

billious

unread,
Feb 27, 2008, 8:18:51 PM2/27/08
to

"Dave R." <dwragle (at) drbsystems (dot) com> wrote in message
news:%232ofBvW...@TK2MSFTNGP04.phx.gbl...

Assuming that the address that you want is the first "Physical Address" (you
didn't say)


[1]SET MAC=
[2]ipconfig /all|find "Physical Address">c:\windows\temp\macaddress.txt
[3]for /f "tokens=2 delims=:" %%i in (c:\windows\temp\macaddress.txt) do IF
NOT DEFINED MAC SET MAC=%%i&echo The MAC Address is %%i

Three lines - each prefixed by [number] for clarity.

OR

[1]SET MAC=
[2]for /f "tokens=2 delims=:" %%i in ( ' ipconfig /all|find "Physical
Address" ' ) do IF NOT DEFINED MAC SET MAC=%%i&echo The MAC Address is %%i

where the single-quotes are required but the spaces surrounding them are
not - they're just there for clarity.

Note that AFTER the last line of these snippets, the MAC address is
available as %MAC%, there is NO spaces EITHER side of the "=" in the SET
statements.

See the FOR documentation for use of the ... in ( ' ... ' ) do ... syntax

for /?

from the prompt

or alt.msdos.batch.nt - where such matters are regularly discussed.


Harlan Grove

unread,
Feb 27, 2008, 8:33:42 PM2/27/08
to
"billious" <billious_1...@hotmail.com> wrote...
...

>Assuming that the address that you want is the first "Physical
>Address" (you didn't say)
>
>[1]SET MAC=
>[2]ipconfig /all|find "Physical Address">
>c:\windows\temp\macaddress.txt
>[3]for /f "tokens=2 delims=:" %%i in
>(c:\windows\temp\macaddress.txt) do IF NOT DEFINED MAC
>SET MAC=%%i&echo The MAC Address is %%i
>
>Three lines - each prefixed by [number] for clarity.
>
>OR
>
>[1]SET MAC=
>[2]for /f "tokens=2 delims=:" %%i in
>( ' ipconfig /all|find "Physical Address" ' ) do IF NOT DEFINED MAC
>SET MAC=%%i&echo The MAC Address is %%i
...

Your approach works for the OP's sample ipconfig output, but it's not
general. Your approach assigns the first physical address in the text
stream to MAC, but the active Ethernet adapter need not be the first
section with a physical address. On my own system, my dial-up VPN
section comes before my regular (Ethernet adapter Local Area
Connection) section.

Anyway, the getmac solution would be the ideal way to go. Learn
something new every day.

Dave R. at dot

unread,
Feb 28, 2008, 7:16:47 AM2/28/08
to

"billious" <billio...@hotmail.com> wrote in message
news:47c60c82$0$28617$a82e...@reader.athenanews.com...

Thanks, but I'm not certain that the Ethernet Adapter will always be the
first physical address, which is why I specified I wanted the MAC
address of the Ethernet adapter.

As to alt.msdos.batch.nt, I would normally have posted there but the
news provider I have available to me at work is down, so for now I'm
limited to the public Microsoft groups.

Regards,

Dave


Dave R. at dot

unread,
Feb 28, 2008, 7:18:10 AM2/28/08
to

"Harlan Grove" <hrl...@gmail.com> wrote in message
news:812ef838-c9df-4f14...@h25g2000hsf.googlegroups.com...

Thanks for the confirmation, I suspected there might be a problem if I
just grabbed the 1st physical address. I'm off to look at the getmac
solution!

Regards,

Dave


Dave R. at dot

unread,
Feb 28, 2008, 7:33:50 AM2/28/08
to

"Harlan Grove" <hrl...@gmail.com> wrote in message
news:ac179d3b-c356-4bb8...@d62g2000hsf.googlegroups.com...

Thanks for the help, Harlan. Ultimately, I think I'll go with the
getmac solution below.

Regards,

Dave


Dave R. at dot

unread,
Feb 28, 2008, 7:46:07 AM2/28/08
to

"Kam-Hung Soh" <kamhu...@gmail.com> wrote in message
news:dc7a6c75-96c7-4662...@i7g2000prf.googlegroups.com...

Thanks for the help! I didn't know about getmac, I'll have to remember
it.

On my sample Vista machine, the output from getmac /v /fo csv was as
follows:

"Connection Name","Network Adapter","Physical Address","Transport Name"
"Local Area Connection","Intel(R) 82566DM-2 Gigabit Network
Connection","00-1A-A0-C3-3C-5D","\Device\Tcpip_{5EEE37DE-FFF9-445E-B3EC-060853AD5897}"

Interesting that it only lists one adapter as opposed to ipconfig
listing 3. Also since Ethernet doesn't appear in the output, findstr
Ethernet wasn't working, but easily remedied.

Here's what I ended up with:

for /f "tokens=3 delims=," %%a in ('"getmac /v /fo csv /nh | findstr
Local"') do echo The MAC Address is %%a

I added /nh to get rid of the header since it isn't useful in this
context, and had findstr look for Local instead of Ethernet. Hopefully,
if any machines end up with multiple lines in their getmac output there
won't be multiple containing the string Local.

Regards,

Dave


billious

unread,
Feb 28, 2008, 9:25:25 AM2/28/08
to

"Dave R." <dwragle (at) drbsystems (dot) com> wrote in message
news:eD79LPge...@TK2MSFTNGP04.phx.gbl...

>
> "billious" <billio...@hotmail.com> wrote in message
> news:47c60c82$0$28617$a82e...@reader.athenanews.com...
>>
[snip]

>
> Thanks, but I'm not certain that the Ethernet Adapter will always be the
> first physical address, which is why I specified I wanted the MAC address
> of the Ethernet adapter.
>
> As to alt.msdos.batch.nt, I would normally have posted there but the news
> provider I have available to me at work is down, so for now I'm limited to
> the public Microsoft groups.
>
> Regards,
>
> Dave
>

Fair 'nuff.

I'd suggest that it would be logical for the report to follow a specific
structure, but then we are talking Microsoft, so there's no guarantee.

You mentioned you'd used XP for your initial approach. My XP/H system
doesn't include GETMAC - no idea whether it's included or a "resource pack"
or "toolkit" inclusion with other editions or versions.

For a version that should work under XP - assuming that the MAC address is
the first "Physical Address" following "Ethernet adapter Local Area
Connection"

----- batch begins -------
[1]@echo off
[2]set mac=&set ealac=
[3]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
/c:"Ethernet adapter" /c:"Physical Address" ' ) do (
[4]if defined ealac if not defined mac set mac=%%j
[5]if not defined ealac if "%%i"=="Ethernet adapter Local Area Connection"
set ealac=Y
[6])
[7]echo MAC address found is %mac%
[8]set ealac=
------ batch ends --------

foxidrive

unread,
Feb 28, 2008, 10:05:58 AM2/28/08
to
On Thu, 28 Feb 2008 22:25:25 +0800, "billious" <billio...@hotmail.com>
wrote:

>I'd suggest that it would be logical for the report to follow a specific
>structure, but then we are talking Microsoft, so there's no guarantee.
>
>You mentioned you'd used XP for your initial approach. My XP/H system
>doesn't include GETMAC - no idea whether it's included or a "resource pack"
>or "toolkit" inclusion with other editions or versions.

Seems that it's an XP Pro tool. A monumentally stupid thing for MS to do
and create multiple versions of an OS, but which they have perpetuated with
Vista. </rant>

>For a version that should work under XP - assuming that the MAC address is
>the first "Physical Address" following "Ethernet adapter Local Area
>Connection"
>
>----- batch begins -------
>[1]@echo off
>[2]set mac=&set ealac=
>[3]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
>/c:"Ethernet adapter" /c:"Physical Address" ' ) do (
>[4]if defined ealac if not defined mac set mac=%%j
>[5]if not defined ealac if "%%i"=="Ethernet adapter Local Area Connection"
>set ealac=Y
>[6])
>[7]echo MAC address found is %mac%
>[8]set ealac=
>------ batch ends --------

It needs some extra logic - here is what mine says:

Ethernet adapter Local Area Connection 4:


Dave R. at dot

unread,
Feb 28, 2008, 10:38:32 AM2/28/08
to

"billious" <billio...@hotmail.com> wrote in message
news:47c699c6$0$2779$a82e...@reader.athenanews.com...

>
> "Dave R." <dwragle (at) drbsystems (dot) com> wrote in message
> news:eD79LPge...@TK2MSFTNGP04.phx.gbl...
>>
>> "billious" <billio...@hotmail.com> wrote in message
>> news:47c60c82$0$28617$a82e...@reader.athenanews.com...
>>>
> [snip]
>>
>> Thanks, but I'm not certain that the Ethernet Adapter will always be
>> the first physical address, which is why I specified I wanted the MAC
>> address of the Ethernet adapter.

<snip>

> I'd suggest that it would be logical for the report to follow a
> specific structure, but then we are talking Microsoft, so there's no
> guarantee.

As Harlan pointed out, there are at least some circumstances where the
Ethernet Adapter isn't the first listed. I suspect there is a
"convention" of sorts that is followed, but it may not be something that
makes sense to you and I.

>
> You mentioned you'd used XP for your initial approach. My XP/H system
> doesn't include GETMAC - no idea whether it's included or a "resource
> pack" or "toolkit" inclusion with other editions or versions.

It is included in XP Pro, so must be one of the things MS decided that
Home users don't need, like Domains. Sigh.

>
> For a version that should work under XP - assuming that the MAC
> address is the first "Physical Address" following "Ethernet adapter
> Local Area Connection"
>
> ----- batch begins -------
> [1]@echo off
> [2]set mac=&set ealac=
> [3]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
> /c:"Ethernet adapter" /c:"Physical Address" ' ) do (
> [4]if defined ealac if not defined mac set mac=%%j
> [5]if not defined ealac if "%%i"=="Ethernet adapter Local Area
> Connection" set ealac=Y
> [6])
> [7]echo MAC address found is %mac%
> [8]set ealac=
> ------ batch ends --------

Works fine on the two XP Pro machines I have available to test. Thanks
for the code!

Regards,

Dave


billious

unread,
Feb 28, 2008, 12:02:54 PM2/28/08
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:dajds3957tnn12u1a...@4ax.com...

> On Thu, 28 Feb 2008 22:25:25 +0800, "billious" <billio...@hotmail.com>
> wrote:
>
>>I'd suggest that it would be logical for the report to follow a specific
>>structure, but then we are talking Microsoft, so there's no guarantee.
>>
>>You mentioned you'd used XP for your initial approach. My XP/H system
>>doesn't include GETMAC - no idea whether it's included or a "resource
>>pack"
>>or "toolkit" inclusion with other editions or versions.
>
> Seems that it's an XP Pro tool. A monumentally stupid thing for MS to do
> and create multiple versions of an OS, but which they have perpetuated
> with
> Vista. </rant>

Thought they learned that lesson with DOS5 (well, let's forget DOS4 - and
the way it's beginning to look, forget Vista, too)

>
>
> It needs some extra logic - here is what mine says:
>
> Ethernet adapter Local Area Connection 4:
>
>

Curious. Any clue as to why "4"? Are there 0..3 as well, or just "4" for the
sake of "4"?

How about

----- batch begins -------
[1]@echo off

[2]setlocal enabledelayedexpansion
[3]set mac=&set ealac=
[4]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr

/c:"Ethernet adapter" /c:"Physical Address" ' ) do (

[5]if defined ealac if not defined mac set mac=%%j
[6]if not defined ealac echo %%i|find "Ethernet adapter Local Area
Connection" >nul&if not errorlevel 1 set ealac=Y
[7])
[8]echo MAC address found is %mac%
------ batch ends --------

(relying on "E.a.L.A.C" being unique for the block of ;ines in question -
but why "4"???)


foxidrive

unread,
Feb 28, 2008, 12:23:04 PM2/28/08
to
On Fri, 29 Feb 2008 01:02:54 +0800, "billious" <billio...@hotmail.com> wrote:

>> It needs some extra logic - here is what mine says:
>>
>> Ethernet adapter Local Area Connection 4:
>
>Curious. Any clue as to why "4"? Are there 0..3 as well, or just "4" for the
>sake of "4"?

Every time you change LAN cards it will increment the number - ditto with Wireless connections.

Test your code with this ipconfig /all output - it did find the right MAC address but as you
can see the description field can contain text that could throw the code:

Windows IP Configuration

Host Name . . . . . . . . . . . . : Number5isalive


Primary Dns Suffix . . . . . . . :

Node Type . . . . . . . . . . . . : Unknown


IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No

DNS Suffix Search List. . . . . . : home

Ethernet adapter VMware Network Adapter VMnet8:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
Physical Address. . . . . . . . . : 00-51-55-C0-32-3E
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.135.1


Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . :

Ethernet adapter VMware Network Adapter VMnet1:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
Physical Address. . . . . . . . . : 00-51-55-C0-EA-DB
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.159.1


Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection 4:

Connection-specific DNS Suffix . : home
Description . . . . . . . . . . . : Atheros L1 Gigabit Ethernet 10/100/1000Base-T Controller
Physical Address. . . . . . . . . : 00-1A-FE-8F-EB-AA
Dhcp Enabled. . . . . . . . . . . : Yes


Autoconfiguration Enabled . . . . : Yes

IP Address. . . . . . . . . . . . : 192.168.1.103
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 192.168.1.1
DHCP Server . . . . . . . . . . . : 192.168.1.1
DNS Servers . . . . . . . . . . . : 202.34.130.33
203.37.230.23
Lease Obtained. . . . . . . . . . : Friday, 29 February 2008 00:17:08
Lease Expires . . . . . . . . . . : Saturday, 1 March 2008 00:17:08


Harlan Grove

unread,
Feb 28, 2008, 1:11:10 PM2/28/08
to
"billious" <billious_1...@hotmail.com> wrote...
...

>Thought they learned that lesson with DOS5 (well, let's forget DOS4
...

Forget DOS4? FWIW, that one was entirely IBM's fault. I remember the
DOS4 shell which had an option to list all files without their
annoying directory paths just like an MVS disk catalog. What an OS!

Mark Blain

unread,
Feb 28, 2008, 5:24:33 PM2/28/08
to
Kam-Hung Soh <kamhu...@gmail.com> wrote in news:dc7a6c75-96c7-4662-8a36-
2c74d6...@i7g2000prf.googlegroups.com:

> Here's a one-liner using "getmac" instead of "ipconfig":
>
> for /f "tokens=3 delims=," %a in ('"getmac /v /fo csv | findstr
> Ethernet"') do echo %a

To download "getmac" and other Resource Kit tools:
http://support.microsoft.com/kb/927229

Mark Blain

unread,
Feb 28, 2008, 5:28:35 PM2/28/08
to
"Dave R." <dwragle (at) drbsystems (dot) com> wrote in
news:eD79LPge...@TK2MSFTNGP04.phx.gbl:

> As to alt.msdos.batch.nt, I would normally have posted there but the
> news provider I have available to me at work is down, so for now I'm
> limited to the public Microsoft groups.

I also frequent that group. You can post to it from groups.google.com,
but some choose not to read any posts from that source.

Dave R. at dot

unread,
Feb 29, 2008, 7:19:12 AM2/29/08
to

"Mark Blain" <mbl...@yahoo.com> wrote in message
news:Xns9A52B206476...@207.46.248.16...

There is that, (although I think most are willing to read, and even
respond if the poster behaves reasonably well), but after a lifetime of
using newsreaders of one variety or another I find the Google Groups web
interface unmanageable. Old dog / new tricks perhaps, but for the odd
post during the (hopefully) short time I'll be without my regular news
provider, if there is an alternative to Google Groups I'll use it.

Regards,

Dave


0 new messages