Skip to main content

How to Get the MAC Address of Network Adapters in Batch Script

The MAC (Media Access Control) address is a unique, hardware-level identifier burned into every network interface card (NIC) by the manufacturer. It's like a physical serial number for your network hardware. Retrieving the MAC address is a common task for network inventory, security scripts (like MAC filtering), or for licensing software to a specific piece of hardware.

This guide will teach you the two most effective built-in commands for getting MAC addresses. We'll cover the simple getmac command, which is great for quick lookups, and the more powerful and detailed ipconfig /all command, which is better for associating a MAC address with a specific adapter.

The Core Command: getmac

The getmac.exe utility is the most direct, built-in tool for this task. It is designed to do one thing: list the MAC addresses (referred to as "Physical Address") for all enabled network adapters on your system.

Syntax: getmac [/V] [/FO <format>]

  • /V: Verbose. Shows the connection name associated with each address.
  • /FO <format>: Format Output. CSV is the best format for scripting.

The More Detailed Command: ipconfig /all

While getmac is good, the ipconfig /all command provides a more complete picture. It lists the MAC address as the "Physical Address" right alongside the adapter's description, IP address, and other configuration details. This makes it the best tool for identifying the MAC address of a specific adapter.

We pipe the output to findstr to isolate the relevant lines: ipconfig /all | findstr "Physical Address"

Basic Example: Displaying All MAC Addresses

This script runs both commands to show the difference in their output.

@ECHO OFF
ECHO --- Getting MAC Addresses ---
ECHO.

ECHO Method 1: Using getmac (simple list)
getmac /V

ECHO.
ECHO ==========================================
ECHO.
ECHO Method 2: Using ipconfig (more detail)
ipconfig /all | findstr "Physical Address"

How to Capture the MAC Address in a Script

To use the MAC address in a script, you need to capture it into a variable. Using the CSV format of getmac is the most reliable way to do this.

This script captures the MAC address of the primary Ethernet adapter.

@ECHO OFF
SET "EthernetMAC="

ECHO --- Capturing Ethernet MAC Address ---

REM 'skip=1' ignores the header. 'tokens=2,3' grabs the Connection Name and MAC.
FOR /F "skip=1 tokens=2,3 delims=," %%A IN ('getmac /V /FO CSV') DO (
REM %%~A removes the quotes from the connection name.
IF /I "%%~A"=="Ethernet" (
SET "EthernetMAC=%%~B"
GOTO :Found
)
)

:Found
IF NOT DEFINED EthernetMAC (
ECHO [FAILURE] Could not find an adapter named "Ethernet".
) ELSE (
ECHO [SUCCESS] The MAC address for the Ethernet adapter is: %EthernetMAC%
)

How the commands work:

  • getmac: This utility directly queries the network adapters through the operating system's hardware abstraction layer to read their factory-assigned physical addresses.
  • ipconfig /all: This command queries the TCP/IP stack for the full configuration of every network interface, which includes the physical address as one of its many properties.

Common Pitfalls and How to Solve Them

Problem: The System Has Multiple Adapters

Modern computers have many network interfaces: Ethernet, Wi-Fi, Bluetooth, and various virtual adapters (for VPNs, virtual machines, etc.). Both getmac and ipconfig will list all of them.

Solution: You cannot assume the first MAC address in the list is the one you want. You must parse the output and look for a specific Connection Name (with getmac /V) or Description (with ipconfig /all). The script in section before demonstrates the correct way to find the adapter you need by name.

Problem: Parsing the Output is Difficult

  • The default table format of getmac and the text output of ipconfig can be tricky to parse if adapter names have spaces.

Solution: For scripting, always use the /FO CSV format for getmac. The comma is a reliable delimiter that makes parsing with FOR /F simple and robust. For ipconfig, you need to be more creative with your FOR /F tokens and delims to grab the correct piece of the line.

Practical Example: A Network Inventory Script

This script creates a simple report that lists every active network adapter and its corresponding MAC and IP address.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO --- Network Adapter Inventory ---
ECHO.

REM This is an advanced FOR loop that parses the detailed ipconfig output.
FOR /F "tokens=1,2* delims=:" %%A IN ('ipconfig /all') DO (
SET "line=%%A"

REM Look for a line that starts with "Ethernet adapter" or "Wireless LAN adapter"
IF "!line:adapter=!" NEQ "!line!" (
ECHO ================================================
ECHO Adapter Name: %%B
)

REM Look for the IPv4 Address line
IF "!line:IPv4 Address=!" NEQ "!line!" (
ECHO IP Address: %%B
)

REM Look for the Physical Address line
IF "!line:Physical Address=!" NEQ "!line!" (
ECHO MAC Address: %%B
)
)
ENDLOCAL

Conclusion

Retrieving the MAC address is a fundamental task for network scripting, and Windows provides excellent built-in tools for the job.

  • The getmac command is the simplest and most direct tool, especially for getting a quick list. For scripting, always use the /FO CSV and /V switches.
  • The ipconfig /all command is the more powerful tool when you need to correlate the MAC address with other details like the adapter's description or its current IP address.

By using these commands and parsing their output with a FOR /F loop, you can easily integrate hardware identification into your network automation scripts.