How to View Network Adapter Configuration in Batch Script
Viewing the detailed configuration of a network adapter is one of the most fundamental tasks in network management and troubleshooting. From a batch script, you might need to get the current IP address, check the default gateway, or verify the DNS server settings before performing a network operation. Windows provides two primary command-line tools for this purpose: the classic ipconfig and the more powerful and script-friendly WMIC.
This guide will teach you how to use both ipconfig and WMIC to view network adapter details. You will learn why WMIC is the superior choice for scripting and see how to parse its output to capture specific information like the IP address and gateway into variables.
The Classic Method (for Humans): ipconfig /all
The ipconfig.exe utility is the go-to tool for quickly viewing TCP/IP configuration. The /all switch provides a detailed, human-readable report for every network interface on the system.
Syntax: ipconfig /all
Its output includes the adapter's description, physical (MAC) address, DHCP status, IP address, subnet mask, default gateway, DNS servers, and more. While perfect for a quick manual check, its multi-line, formatted text is very difficult to parse reliably in a script.
The Modern Method (for Scripting): WMIC NICCONFIG
The WMIC (Windows Management Instrumentation Command-line) utility can query system components as if they were objects in a database. This provides structured, predictable output that is ideal for scripting.
Syntax: WMIC NICCONFIG GET Description, IPAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder
NICCONFIG: The WMI alias for theWin32_NetworkAdapterConfigurationclass.GET ...: The specific properties you want to retrieve.
Basic Example: Displaying the Configuration
This script runs both commands to contrast their output styles.
@ECHO OFF
ECHO --- Method 1: Using ipconfig /all (Human-Readable) ---
ECHO.
ipconfig /all
ECHO.
ECHO ==========================================================
ECHO.
ECHO --- Method 2: Using WMIC (Script-Friendly Table) ---
ECHO.
WMIC NICCONFIG GET Description, IPAddress, DefaultIPGateway
How to Capture Specific Information in a Script
To use this data in a script, you must parse the command's output. This is where WMIC's superiority shines, especially when formatted as CSV.
This script uses WMIC to find the active network connection and capture its IP address and gateway.
@ECHO OFF
SETLOCAL
SET "IP_Address="
SET "Gateway="
ECHO --- Capturing Active Network Configuration ---
REM 'skip=1' ignores the header line. 'tokens=1,2' grabs the first two data columns.
FOR /F "skip=1 tokens=1,2 delims=," %%A IN (
'WMIC NICCONFIG WHERE "IPEnabled=TRUE" GET DefaultIPGateway, IPAddress /FORMAT:CSV'
) DO (
SET "Gateway=%%A"
SET "IP_Address=%%B"
GOTO :InfoCaptured
)
:InfoCaptured
IF NOT DEFINED IP_Address (
ECHO [FAILURE] Could not find an active network adapter.
) ELSE (
ECHO.
ECHO Captured IP Address: %IP_Address%
ECHO Captured Gateway: %Gateway%
)
ENDLOCAL
WHERE "IPEnabled=TRUE": This is a crucial WMI filter that tells it to return information only for network adapters that are currently active and have an IP address./FORMAT:CSV: This makes the output a simple, comma-separated string, which is trivial to parse.
How the Methods ipconfig and WMIC Work
ipconfig: This is a high-level utility that communicates directly with the TCP/IP networking stack to get its current state and formats it into a text report.WMIC: This is a query engine. It asks the WMI service for all objects of theWin32_NetworkAdapterConfigurationclass and then displays the requested properties of those objects. It is a more structured and database-like approach.
Common Pitfalls and How to Solve Them
Problem: The System Has Multiple Adapters
A typical computer has many network interfaces (Ethernet, Wi-Fi, Bluetooth, virtual adapters for VPNs or VMs). ipconfig /all will list all of them, creating a lot of noise.
Solution: The WMIC WHERE clause is the perfect solution. By using WHERE "IPEnabled=TRUE", you can instantly filter out all the inactive and irrelevant adapters and focus only on the one that is currently connected to the network.
Problem: Parsing the ipconfig Output is Difficult
The output of ipconfig is not arranged in a simple table. A single adapter's information is spread across multiple lines, and the labels are padded with a variable number of dots, making it extremely difficult to parse with a FOR /F loop.
Solution: Do not try to parse ipconfig in a script. It is fragile and will break if the language or formatting changes. Always use WMIC for any automated task that needs to read network configuration.
Practical Example: A Detailed Network Report Script
This script uses WMIC to generate a clean, detailed report for all active network adapters and saves it to a file.
@ECHO OFF
SETLOCAL
SET "REPORT_FILE=%USERPROFILE%\Desktop\Network_Report.txt"
ECHO --- Network Configuration Reporter ---
ECHO Creating report at: "%REPORT_FILE%"
(
ECHO Network Report for: %COMPUTERNAME%
ECHO Generated on: %DATE% @ %TIME%
ECHO ==========================================================
ECHO.
) > "%REPORT_FILE%"
REM Use WMIC with a WHERE clause and format the output.
WMIC NICCONFIG WHERE "IPEnabled=TRUE" GET Description, IPAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder, MACAddress /FORMAT:LIST >> "%REPORT_FILE%"
ECHO.
ECHO [SUCCESS] Report created.
START "" "%REPORT_FILE%"
ENDLOCAL
/FORMAT:LIST creates a nice, human-readable key=value list, which is great for log files.
Conclusion
While ipconfig is the go-to tool for a quick manual check, WMIC is the professional standard for scripting network configuration queries.
- Use
ipconfig /allfor interactive, human-readable diagnostics. - Use
WMIC NICCONFIGfor all batch script automation. - Leverage the
WHERE "IPEnabled=TRUE"clause to easily find the active network adapter. - Use the
/FORMAT:CSVor/FORMAT:LISTswitches to produce clean, predictable output that is easy to work with in your scripts.