Windows 10

Batch File To Get MAC Address

In this tutorial, we’ll learn how to retrieve the MAC address (also known as the Physical Address) of a network adapter using a batch script. This is especially useful in environments where device identification, network inventory, or automated diagnostics are required.

Batch files are commonly used to execute a sequence of system commands automatically, and they provide an efficient way to query system information like network interface details.
 

📜 Example: Batch File to Retrieve MAC Address

The following example filters the output of ipconfig /all to display only the MAC address using the find command.

@echo off
ipconfig /all | find "Physical Address" /i

🧾 Sample Output:

Physical Address. . . . . . . . . : 9A-16-12-49-3E-62

🔍 Explanation:

  • ipconfig /all shows detailed network configuration for all adapters.
  • | pipes the output into the next command.
  • find “Physical Address” searches for lines that contain the phrase “Physical Address” (case-insensitive due to the /i flag).
  • This extracts only the lines showing MAC addresses, making the output easier to parse or log.

 

🛠 Advanced Use: Store MAC Address in a Variable

To programmatically use the MAC address in your script, you can extract it using a for /f loop:

@echo off
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^| find "Physical Address"') do (
    set mac=%%a
    goto show
)
:show
echo Your MAC address is: %mac%
Note: This will capture only the first MAC address found, which is sufficient for most single-adapter systems.

 

✅ Summary

This method provides a simple and scriptable way to retrieve the MAC address on Windows systems using a batch file. It’s particularly handy for IT professionals, system administrators, or developers working with device registration or network automation.
 

Leave a Reply

Your email address will not be published. Required fields are marked *