Skip to main content

How to Get the Subnet Mask of a Network Adapter in Batch Script

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, and that text changes depending on the Windows display language.

This guide will explain several methods for retrieving the subnet mask of your active network adapter, with a focus on making scripts work across all Windows languages.

Method 1: Parsing IPCONFIG with a Language-Independent Pattern

The ipconfig output labels change with the Windows display language ("Subnet Mask" in English, "Maschera di sottorete" in Italian, "Subnetzmaske" in German). However, the value format is always the same: a dotted-decimal mask like 255.255.255.0 that appears after a colon on its own line. The key insight is that the subnet mask almost always starts with 255., unlike IP addresses or gateways, which rarely do. We can exploit this to build a pattern that works in every language.

@echo off
setlocal

rem --- Search for any ipconfig line whose value after ":" starts with "255." ---
for /f "tokens=2 delims=:" %%a in (
'ipconfig ^| findstr /r /c:"255\.[0-9]"'
) do (
rem --- Trim leading spaces from the token ---
for /f "tokens=1" %%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

How it works:

  1. delims=: splits each ipconfig line at the colon. Token 2 is the value portion (e.g., 255.255.255.0).
  2. findstr /r /c:"255\.[0-9]" matches only lines containing a value that includes 255. followed by a digit, this is the subnet mask.
  3. The inner FOR /F "tokens=1" trims the leading spaces that ipconfig adds after the colon.
  4. goto :ShowResult stops after the first match, which handles machines with multiple adapters.
warning

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. If you need a specific adapter, see Method 2.

Method 2: Detailed Search for a Specific Adapter

If you need the mask for a specific adapter (like "Ethernet"), you must isolate that section of the ipconfig output. Adapter headers in ipconfig are not indented, while the detail lines beneath them are. We use this structural difference, combined with the 255. value trick, to stay language-independent.

@echo off
setlocal enabledelayedexpansion
set "AdapterName=Ethernet"

set "FoundAdapter=false"
for /f "tokens=* delims=" %%x in ('ipconfig') do (
rem --- Adapter headers contain the adapter name and are not indented ---
echo %%x | findstr /i /c:"%AdapterName%" >nul && (
set "FoundAdapter=true"
)

rem --- A blank line resets the section (new adapter or gap) ---
set "Line=%%x"
if "!Line: =!"=="" (
rem --- Only reset if we haven't found our mask yet ---
if not defined FinalMask set "FoundAdapter=false"
)

if "!FoundAdapter!"=="true" (
rem --- Match the subnet mask value (starts with 255.) regardless of label ---
echo %%x | findstr /r /c:"255\.[0-9]" >nul && (
for /f "tokens=2 delims=:" %%g in ("%%x") do (
for /f "tokens=1" %%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

How it works:

  1. When a line contains the adapter name, a flag is set.
  2. Subsequent lines in that section are checked for a value matching 255.x.
  3. A blank line (which ipconfig uses to separate adapter sections) resets the flag so that a mask from a different adapter is never captured.
  4. The colon delimiter and inner FOR trim extract just the clean mask value.

wmic returns structured data using the same property names in every language. The property IPSubnet is always called IPSubnet whether Windows is in English, German, Japanese, or any other language. This makes it the most reliable method for multilanguage environments.

@echo off
setlocal enabledelayedexpansion

set "mask="
for /f "skip=1 tokens=*" %%a in (
'wmic nicconfig where "IPEnabled=True" get IPSubnet 2^>nul'
) do (
rem --- WMIC output contains trailing carriage returns; clean them ---
set "Line=%%a"
set "Line=!Line: =!"
if not "!Line!"=="" if not defined mask (
rem --- Extract the actual mask from the WMIC array format {"x.x.x.x"} ---
set "raw=%%a"
set "raw=!raw:{=!"
set "raw=!raw:}=!"
set "raw=!raw:"=!"
rem --- Handle arrays: take only the first element (before comma) ---
for /f "tokens=1 delims=," %%m in ("!raw!") do (
for /f "tokens=1" %%n in ("%%m") do set "mask=%%n"
)
)
)

if defined mask (
echo Subnet Mask: %mask%
) else (
echo [ERROR] No active adapter found.
)
pause

How it works:

  1. wmic nicconfig where "IPEnabled=True" get IPSubnet returns the subnet mask for all active adapters.
  2. WMIC formats array values as {"255.255.255.0"} or {"255.255.255.0", "64"} (when both IPv4 and IPv6 prefix lengths are present). The script strips curly braces and quotes.
  3. tokens=1 delims=, isolates the first element (the IPv4 mask).
  4. WMIC output lines end with a carriage return character that can cause phantom matches; the blank-line check filters those out.
info

WMIC Deprecation Notice: Microsoft has marked wmic.exe as deprecated starting in Windows 10 21H1. It still works in current Windows versions, but for future-proof scripts, consider PowerShell's Get-CimInstance or the ipconfig-based methods above.

How to Avoid Common Errors

Wrong Way: Hardcoding Label Text

Never search for the literal string "Subnet Mask". It only works on English-language Windows.

rem *** BAD: breaks on non-English Windows ***
ipconfig | findstr "Subnet Mask"

Correct Way: Match the value pattern (255.) or use WMIC/PowerShell property names that never change.

Wrong Way: Hardcoding Token Positions

In some languages or Windows versions, the label before the colon has a different number of words, shifting the token count.

rem *** BAD: "tokens=3" assumes exactly two words before the value ***
for /f "tokens=3" %%a in ('ipconfig ^| findstr "Subnet"') do ...

Correct Way: Use delims=: to split at the colon. The value is always on the right side of it, regardless of how many words the label contains.

Problem: Multiple Masks (IPv4 vs. IPv6)

WMIC's IPSubnet can return an array containing both the IPv4 subnet mask and the IPv6 prefix length. Always take only the first comma-delimited element and verify it looks like a dotted-decimal address.

Best Practices and Rules

1. Never Match on Localized Labels

The words ipconfig uses to describe fields change with the Windows display language. Build your logic around value patterns (like 255. for masks) or use tools whose property names are language-independent (wmic, PowerShell).

2. Always Use Colon as the Delimiter for ipconfig

Every detail line in ipconfig follows the format Label . . . : Value. Splitting on : gives you the value reliably, regardless of how many words or dots the label contains.

3. Identify the Gateway Too

Usually, if you need the subnet mask, you also need the Default Gateway. You can apply the same delims=: technique; just match a different value pattern (the gateway will not start with 255.).

4. Handle Empty Results Gracefully

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.

5. Prefer WMIC or PowerShell for Automation

For scripts that must run unattended on machines of unknown language, wmic (or PowerShell's Get-CimInstance Win32_NetworkAdapterConfiguration) is the safest choice.

Conclusions

Getting the subnet mask in a Batch script is a fundamental networking task. While ipconfig is the most accessible tool, its output is language-dependent. The critical fix is to stop matching on translated labels and instead match on the value pattern (the mask always starts with 255.) or use WMIC, whose property names are identical in every language. By combining these techniques with proper adapter isolation and empty-result handling, you can build automation that works reliably on any Windows machine worldwide.