How to Get the Subnet Mask of a Network Adapter
In networking automation, knowing the IP address is only half the story. The "Subnet Mask" defines the boundaries of your local network and determines which IP addresses can be reached directly and which require a gateway. If you are writing a script to configure static IPs, verify network consistency, or troubleshoot connectivity, you need a reliable way to extract this value. While ipconfig shows the mask, parsing that text in a Batch script requires careful string manipulation.
This guide will explain several methods for retrieving the subnet mask of your active network adapter.
Method 1: Parsing IPCONFIG (The Common Way)β
This method searches the output of ipconfig for the "Subnet Mask" line and captures the value.
@echo off
setlocal
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /i "Subnet"') do (
:: Trim leading spaces using a secondary loop
for /f "tokens=*" %%b in ("%%a") do set "CurrentMask=%%b"
goto :ShowResult
)
:ShowResult
if defined CurrentMask (
echo [NET] detected Subnet Mask: %CurrentMask%
) else (
echo [ERROR] Subnet Mask not found.
)
pause
If a computer has multiple adapters (e.g., Wi-Fi and Ethernet), findstr will return multiple results. This script handles that by using goto to stop after finding the very first one. The regex ensures only lines with an actual IP-format value are matched.
Method 2: Detailed Search for a Specific Adapterβ
If you need the mask for a specific adapter (like "Ethernet"), you must use a more complex FOR loop to isolate that section of the ipconfig output.
@echo off
setlocal enabledelayedexpansion
set "AdapterName=Ethernet"
set "FoundAdapter=false"
for /f "delims=" %%x in ('ipconfig') do (
set "Line=%%x"
rem Ipconfig headings (e.g. "Ethernet adapter Ethernet:") have no leading spaces.
rem We can check the first character to know when a new adapter section starts!
set "FirstChar=!Line:~0,1!"
if "!FirstChar!" neq " " (
echo !Line! | findstr /i /c:"%AdapterName%" >nul && set "FoundAdapter=true" || set "FoundAdapter=false"
)
if "!FoundAdapter!"=="true" (
echo !Line! | findstr /i "Subnet Subnetz Masque MΓ‘scara" >nul && (
for /f "tokens=2 delims=:" %%g in ("!Line!") do (
rem Trim leading spaces
for /f "tokens=*" %%h in ("%%g") do set "FinalMask=%%h"
)
goto :Done
)
)
)
:Done
if defined FinalMask (
echo Adapter: %AdapterName% Mask: !FinalMask!
) else (
echo [ERROR] Subnet Mask not found for %AdapterName%.
)
pause
Method 3: Using WMIC (Variable-Ready)β
wmic is often cleaner for Batch scripts because it returns data in a structured way without the "human-readable" headers of ipconfig.
@echo off
setlocal
for /f "skip=1 tokens=*" %%a in ('wmic nicconfig where "IPEnabled=True" get IPSubnet') do (
:: WMIC outputs string arrays as {"IP", "SubnetPrefix"}
:: We use delimiters { and , to slice it, then ~ to slice the quotes!
for /f "tokens=1 delims={,}" %%b in ("%%a") do (
set "mask=%%~b"
goto :Done
)
)
:Done
if defined mask (
echo Subnet Mask: %mask%
) else (
echo [ERROR] No active adapter found.
)
pause
How to Avoid Common Errorsβ
Wrong Way: Hardcoding Token Positionsβ
In some versions of Windows (or different languages), the word "Subnet Mask" might be on a different line or have a different number of spaces.
Correct Way: Use the colon (:) as your delimiter in the FOR loop and always trim the leading space from the result.
Problem: Multiple Masks (IPv4 vs. IPv6)β
WMI can sometimes return an array of masks. Ensure you are targeting the IPEnabled=True filter to only look at active, functional adapters.
Best Practices and Rulesβ
1. Account for Localizationβ
In a non-English version of Windows, the word "Subnet Mask" will be translated (e.g., "Maschera di sottorete" in Italian). If your script needs to work internationally, use Method 3 (WMIC), as it uses the same variable names (IPSubnet) in every language.
2. Identify the Gatewayβ
Usually, if you need the subnet mask, you also need the Gateway. You can use the same FOR loop logic to search for "Default Gateway."
3. Handle Empty Resultsβ
If the machine is disconnected from the network, ipconfig will not show a subnet mask. Always include an if not defined check to prevent the script from continuing with invalid data.
Final Thoughtsβ
Getting the subnet mask in a Batch script is a fundamental networking task. While ipconfig is easy to read, WMIC offers a more robust and language-independent solution for professional scripts. By correctly parsing these outputs and handling multiple adapters, you can build automation that is aware of its local network boundaries, enabling more complex tasks like static IP assignment or automated network auditing.