How to Get the Current Wi-Fi Network Name (SSID) in Batch Script
In a world of multiple office networks, home Wi-Fi, and public hotspots, your script needs to know which network it is connected to. If your backup script is on the "Home" network, it might be safe to run, but if it's on "Starbucks Guest Wi-Fi," you should probably block it to protect your data. Identifying the SSID (Service Set Identifier) allows you to create location-aware Batch scripts that automatically adjust their security settings, mapped drives, and printer configurations based on your physical location.
This guide will explain how to extract the current SSID using netsh.
Method 1: Parsing netsh wlan (Most Reliable)
The netsh wlan show interfaces command provides a detailed report of your wireless status, including the SSID of the currently connected network. The challenge is that the label before the colon changes with the Windows display language, but the field name SSID remains consistent. We must be careful not to match BSSID (the router's MAC address) by accident.
@echo off
setlocal enabledelayedexpansion
echo [NET] Identifying current Wi-Fi network...
set "CurrentSSID="
rem --- Parse netsh output using colon as delimiter ---
rem --- findstr /r matches lines containing " SSID" preceded by a space or ---
rem --- start of meaningful content, but NOT lines containing "BSSID" ---
for /f "tokens=1,* delims=:" %%a in (
'netsh wlan show interfaces 2^>nul ^| findstr /r /c:"^ *SSID"'
) do (
rem --- Exclude BSSID lines: check if the label contains "BSSID" ---
echo %%a | findstr /i /c:"BSSID" >nul
if !errorlevel! neq 0 (
if not defined CurrentSSID (
set "CurrentSSID=%%b"
rem --- Trim the leading space after the colon ---
if defined CurrentSSID set "CurrentSSID=!CurrentSSID:~1!"
)
)
)
if not defined CurrentSSID (
echo [INFO] No Wi-Fi connection detected. You may be on Ethernet or disconnected.
) else (
echo [RESULT] Connected to^: !CurrentSSID!
)
endlocal
pause
How it works:
netsh wlan show interfacesoutputs all wireless adapter details.findstr /r /c:"^ *SSID"matches lines whereSSIDappears near the start (with optional leading spaces), capturing both theSSIDline and theBSSIDline.- The inner
findstr /i /c:"BSSID"check excludes any line whose label containsBSSID, ensuring only the network name is captured. delims=:splits at the colon. Token%%b(the*remainder) captures everything after the colon, including SSID names that contain colons or special characters.set "CurrentSSID=!CurrentSSID:~1!"trims the single leading space thatnetshplaces after the colon.
SSID names can contain spaces, special characters, and even exclamation marks. Using delims=: with tokens=1,* ensures the full SSID is captured even if it contains spaces. However, SSIDs containing ! will conflict with delayed expansion. If you must handle such edge cases, consider writing the netsh output to a temporary file and processing it with delayed expansion disabled.
Method 2: Location-Aware Network Guard
If you want to perform a specific action only when connected to a known network, you can combine SSID detection with conditional logic. This version extracts the SSID cleanly first, then compares it, which is more reliable than searching the raw netsh output for the target name.
@echo off
setlocal enabledelayedexpansion
set "TargetSSID=Office_Secure_WiFi"
set "CurrentSSID="
rem --- Extract the current SSID ---
for /f "tokens=1,* delims=:" %%a in (
'netsh wlan show interfaces 2^>nul ^| findstr /r /c:"^ *SSID"'
) do (
echo %%a | findstr /i /c:"BSSID" >nul
if !errorlevel! neq 0 (
if not defined CurrentSSID (
set "CurrentSSID=%%b"
if defined CurrentSSID set "CurrentSSID=!CurrentSSID:~1!"
)
)
)
rem --- Compare against the target ---
if not defined CurrentSSID (
echo [WARN] No Wi-Fi connection detected. Aborting corporate operations.
goto :End
)
echo [INFO] Current network: !CurrentSSID!
if /i "!CurrentSSID!"=="!TargetSSID!" (
echo [AUTH] Verified: You are on the secure office network.
echo [NET] Mapping corporate drives...
net use Z: \\server\share /persistent:no >nul 2>&1
if !errorlevel! equ 0 (
echo [SUCCESS] Drive Z: mapped to \\server\share
) else (
echo [ERROR] Failed to map drive. Check server availability.
)
) else (
echo [DENIED] You are on "!CurrentSSID!", not "!TargetSSID!".
echo [DENIED] Corporate drives will not be mapped.
)
:End
endlocal
pause
Method 3: Auditing Connection Quality
While getting the network name, you can also extract the signal quality percentage and connection speed to determine if your connection is stable enough for intensive operations like large file transfers.
@echo off
setlocal enabledelayedexpansion
echo [AUDIT] Wi-Fi Connection Quality Report
echo ========================================
echo.
set "SSID="
set "Signal="
set "Speed="
set "State="
for /f "tokens=1,* delims=:" %%a in (
'netsh wlan show interfaces 2^>nul'
) do (
rem --- Trim leading spaces from the label for comparison ---
set "Label=%%a"
rem --- Match SSID (but not BSSID) ---
echo %%a | findstr /r /c:"^ *SSID" >nul
if !errorlevel! equ 0 (
echo %%a | findstr /i /c:"BSSID" >nul
if !errorlevel! neq 0 (
if not defined SSID (
set "SSID=%%b"
if defined SSID set "SSID=!SSID:~1!"
)
)
)
rem --- Match Signal quality ---
echo %%a | findstr /i /c:"Signal" >nul
if !errorlevel! equ 0 (
set "Signal=%%b"
if defined Signal set "Signal=!Signal:~1!"
)
rem --- Match Receive/Transmit rate ---
echo %%a | findstr /i /c:"Receive rate" >nul
if !errorlevel! equ 0 (
set "Speed=%%b"
if defined Speed set "Speed=!Speed:~1!"
)
rem --- Match connection State ---
echo %%a | findstr /i /c:"State" >nul
if !errorlevel! equ 0 (
if not defined State (
set "State=%%b"
if defined State set "State=!State:~1!"
)
)
)
if not defined SSID (
echo [INFO] No Wi-Fi connection detected.
) else (
echo Network: !SSID!
echo State: !State!
echo Signal: !Signal!
echo Speed: !Speed!
)
endlocal
pause
Internationalization Note: The labels "Signal", "Receive rate", and "State" are translated in non-English Windows versions (e.g., "Segnale" in Italian, "Empfangen" in German). If you need this script to work across languages, you can match by line position or by value pattern instead of label text. The SSID field name itself is consistent across languages.
How to Avoid Common Errors
Wrong Way: Matching BSSID by Mistake
The netsh output contains both SSID (the human-readable network name) and BSSID (the router's hardware MAC address). A naive findstr "SSID" matches both lines.
rem *** BAD: matches both SSID and BSSID ***
netsh wlan show interfaces | findstr "SSID"
Correct Way: Match the SSID label specifically and then exclude lines containing BSSID.
Wrong Way: Using tokens=2 Instead of tokens=1,*
If the SSID contains a colon (rare but valid), tokens=2 would only capture the portion before the first colon in the SSID name.
rem *** BAD: truncates SSIDs containing colons ***
for /f "tokens=2 delims=:" %%a in (...) do set "SSID=%%a"
Correct Way: Use tokens=1,* so that %%b captures everything after the first colon, preserving the full SSID.
Problem: Multiple Wi-Fi Adapters
If a laptop has two wireless adapters (e.g., internal card and a USB dongle), netsh wlan show interfaces lists both. The script may capture the SSID from the wrong adapter.
Solution: Use the adapter name to target a specific interface:
rem --- Show only the adapter named "Wi-Fi" ---
netsh wlan show interfaces name="Wi-Fi"
Problem: Wi-Fi Service Not Running
If the WLAN AutoConfig service is stopped, netsh wlan show interfaces returns an error. The 2>nul redirect in the scripts above suppresses this, and the if not defined CurrentSSID check handles the empty result gracefully.
Best Practices and Rules
1. Empty SSID Does Not Mean No Internet
If your script returns an empty SSID, the computer might still have internet access through Ethernet. Always check for wired connectivity as a fallback before concluding the machine is offline.
2. The SSID Label Is Language-Consistent
Unlike most netsh output labels, the SSID and BSSID field names remain the same across all Windows display languages because they are industry-standard acronyms. However, other fields like "Signal", "State", and "Radio type" are translated.
3. Log the Network for Security Audits
Recording which SSID a machine connects to helps identify employees using unsecured public hotspots instead of the corporate network.
echo %date% %time% - %COMPUTERNAME% connected to: %CurrentSSID% >> "%~dp0wifi_audit.log"
4. Use setlocal / endlocal
Always wrap scripts in setlocal and endlocal to prevent variables from persisting beyond the script's execution.
Conclusions
Extracting the current SSID transforms your Batch scripts into intelligent, location-aware tools. By identifying which network you are connected to, you can automate security decisions, mapping corporate drives only on trusted networks, blocking sensitive operations on public Wi-Fi, and logging connection details for compliance audits. The key technical challenges are avoiding the BSSID trap, handling SSIDs with special characters, and accounting for machines with multiple wireless adapters. With these safeguards in place, your scripts can reliably adapt their behavior based on the network environment.