How to Get the System's MAC Address in a Batch Script
A MAC (Media Access Control) address is a unique, 12-character hexadecimal identifier assigned to a network interface controller (NIC) for use in network communications. Scripts often need to retrieve a machine's MAC address for logging, inventory, or for use in licensing or network access control systems.
This guide will teach you the two primary, built-in commands for retrieving MAC addresses in Windows: the simple and direct getmac command, and the more powerful but complex ipconfig command. You will learn how to parse their output to get a clean MAC address into a variable for use in your scripts.
What is a MAC Address?
A MAC address is a hardware address that uniquely identifies each node of a network. It is typically represented as six groups of two hexadecimal digits, separated by hyphens or colons.
- Hyphen Format:
00-1A-2B-3C-4D-5E - Colon Format:
00:1A:2B:3C:4D:5E
A single computer can have multiple MAC addresses, one for its Ethernet port, one for its Wi-Fi adapter, one for its Bluetooth adapter, etc.
Method 1 (Recommended): The getmac Command
The getmac.exe utility is the simplest and most direct tool for this task. Its entire purpose is to list the MAC addresses of the network adapters in the system.
Command: getmac
For scripting, its CSV (Comma-Separated Value) format is the easiest to parse: getmac /FO CSV /NH
/FO CSV: Formats the Output as CSV./NH: No Header. Suppresses the header row.
This getmac /FO CSV /NH command produces a clean, quoted, comma-separated list.
"Realtek PCIe GbE Family Controller","Ethernet","00-1A-2B-3C-4D-5E"
"Intel(R) Wi-Fi 6 AX201 160MHz","Wi-Fi","F8-6A-7B-8C-9D-0E"
"Bluetooth Device (Personal Area Network)","Bluetooth Network Connection","E4-5A-6B-7C-8D-9F"
Method 2 (Alternative): Parsing ipconfig /all
The ipconfig /all command provides a huge amount of detail about every network adapter, including its "Physical Address" (the MAC address).
Why getmac is better:
- Simpler Output: The output of
getmacis far cleaner and more structured, making it much easier to parse in a script. - More Direct:
ipconfigproduces a lot of extra information that you have to filter out.
The ipconfig method should only be used if getmac is not available for some reason (which is extremely rare on modern systems).
The Script: Getting the MAC Address with getmac
This script uses the recommended getmac command to get the MAC address of the primary, active network connection (usually the Ethernet or Wi-Fi that is currently connected).
@ECHO OFF
SETLOCAL
SET "MacAddress="
ECHO --- Getting the primary MAC address ---
ECHO.
REM This FOR /F loop processes the clean output from getmac.
REM We look for a line that does NOT contain "Disconnected".
FOR /F "tokens=1" %%A IN ('getmac /FO CSV /NH ^| FINDSTR /V "Disconnected"') DO (
REM The first token is the MAC address, with quotes.
REM We use the ~ modifier to remove the quotes.
SET "MacAddress=%%~A"
REM We use GOTO to exit the loop after grabbing the first address.
GOTO :Found
)
:Found
IF NOT DEFINED MacAddress (
ECHO [ERROR] Could not find an active network adapter.
) ELSE (
ECHO The primary MAC address is: %MacAddress%
)
ENDLOCAL
How the getmac Script Works
getmac /FO CSV /NH: This generates the clean, CSV-formatted list.^| FINDSTR /V "Disconnected": This is a crucial filter. We pipe the output ofgetmactoFINDSTRand use/Vto show only the lines that do NOT contain the word "Disconnected". This filters out adapters that are not currently active.FOR /F "tokens=1": The loop processes this filtered output. We only care about the first token, which in the CSV output is the MAC address.SET "MacAddress=%%~A":%%Awill contain the MAC address, but with quotes (e.g.,"00-1A-2B-..."). The~modifier removes the surrounding quotes, giving us a clean value.GOTO :Found: Since we only want the first active adapter, we useGOTOto break out of the loop immediately after processing the first line.
Common Pitfalls and How to Solve Them
-
Multiple MAC Addresses: A system can have many adapters. The script above makes an assumption that the first active adapter it finds is the "primary" one (which is usually the case for Ethernet/Wi-Fi). If you need to identify a specific adapter, you would need more complex parsing logic to check the adapter name (the second token from
getmac). -
Virtual Adapters: Virtualization software (like VMware, Hyper-V, or VirtualBox) and VPN clients create virtual network adapters. These will also appear in the
getmaclist. TheFINDSTR /V "Disconnected"filter helps, as these are often disconnected when not in use.
Practical Example: A System Inventory Script
This script gathers several key system identifiers, including the computer name, serial number, and primary MAC address, and saves them to an inventory file.
@ECHO OFF
SETLOCAL
SET "InventoryFile=%USERPROFILE%\Desktop\System_Inventory.txt"
SET "MacAddress="
ECHO --- System Inventory Tool ---
ECHO Gathering information...
REM --- Get the MAC Address ---
FOR /F "tokens=1" %%A IN ('getmac /FO CSV /NH ^| FINDSTR /V "Disconnected"') DO (
SET "MacAddress=%%~A"
GOTO :MacFound
)
:MacFound
REM --- Get the System Serial Number ---
FOR /F "skip=1" %%S IN ('WMIC BIOS GET SerialNumber') DO (
SET "SerialNumber=%%S"
GOTO :SerialFound
)
:SerialFound
REM --- Write the report ---
(
ECHO System Inventory Report
ECHO Generated on: %DATE% %TIME%
ECHO ==================================
ECHO Computer Name: %COMPUTERNAME%
ECHO Serial Number: %SerialNumber%
ECHO MAC Address: %MacAddress%
) > "%InventoryFile%"
ECHO.
ECHO [SUCCESS] Inventory report saved to your desktop.
ECHO "%InventoryFile%"
ENDLOCAL
Conclusion
Getting a system's MAC address is a straightforward task with the right built-in tools.
- The
getmaccommand is the simple, direct, and recommended method. Using it with the/FO CSV /NHswitches provides clean, script-friendly output. - The
ipconfig /allcommand is a valid but more complex alternative that is generally not needed. - The most robust scripting pattern is to filter the output of
getmacwithFINDSTRto isolate active connections and then parse the result with aFOR /Floop.
By using this technique, you can reliably retrieve a machine's hardware address for use in any logging, inventory, or network management script.