Skip to main content

How to Connect to a Wi-Fi Network in Batch Script

Automating your network connectivity is essential for mobile workstations, IoT devices, and laptop deployments. Rather than waiting for Windows to find the right signal, a Batch script can force an immediate connection to a specific SSID. This is especially useful for setting up location-aware machines: for example, a script that detects you are in the lab and instantly switches from the weak office Wi-Fi to a dedicated high-speed laboratory network. A Batch script uses the netsh wlan command to manage these connections.

This guide will explain how to programmatically connect to Wi-Fi.

Method 1: Connecting to a Saved Profile

Windows can only connect to networks it already knows: networks you have connected to before and whose credentials are stored as a profile. The netsh wlan connect command references these saved profiles by name.

@echo off
setlocal enabledelayedexpansion

set "SSID=Office_Internal_WiFi"

rem --- Verify the Wireless AutoConfig service is running ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] The Wireless AutoConfig service is not running.
echo [INFO] Start it with: net start WlanSvc
endlocal
pause
exit /b 1
)

rem --- Verify the profile exists before attempting connection ---
netsh wlan show profiles 2>nul | findstr /i /c:"%SSID%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] No saved profile found for "%SSID%".
echo [INFO] Connect to this network manually first, or import a profile XML.
echo [INFO] Available profiles:
netsh wlan show profiles
endlocal
pause
exit /b 1
)

echo [ACTION] Connecting to "%SSID%"...

rem --- Send the connection command ---
netsh wlan connect name="%SSID%"

if %errorlevel% neq 0 (
echo [ERROR] Connection command failed for "%SSID%".
endlocal
pause
exit /b 1
)

echo [INFO] Connection command sent. Waiting for handshake and DHCP...

rem --- Wait for the Wi-Fi handshake and IP assignment ---
timeout /t 8 /nobreak >nul

rem --- Verify the connection actually succeeded ---
set "ConnectedSSID="
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 ConnectedSSID (
set "ConnectedSSID=%%b"
if defined ConnectedSSID set "ConnectedSSID=!ConnectedSSID:~1!"
)
)
)

if /i "!ConnectedSSID!"=="%SSID%" (
echo [SUCCESS] Connected to "%SSID%".
) else if defined ConnectedSSID (
echo [WARN] Connected to "!ConnectedSSID!" instead of "%SSID%".
echo [WARN] Windows may have switched to a higher-priority network.
) else (
echo [WARN] Connection command was accepted but no SSID is active.
echo [WARN] The network may be out of range or the password may be invalid.
)

endlocal
pause

Key details:

  1. Profile check first: netsh wlan connect only works with saved profiles. The script verifies the profile exists before attempting connection, giving a clear error if it does not.
  2. Post-connection verification: netsh wlan connect returns errorlevel 0 when the command is sent, not when the connection succeeds. The script waits 8 seconds, then checks netsh wlan show interfaces to confirm the actual SSID.
  3. BSSID exclusion: When reading the connected SSID from show interfaces, the script excludes BSSID lines to avoid capturing the router's MAC address instead of the network name.
warning

The netsh wlan connect command does not accept a password as an argument. It only works with profiles that are already saved in the Windows wireless database. To create a profile, either connect manually once through the Windows UI, or import a profile XML file using netsh wlan add profile filename="profile.xml".

Method 2: Targeting a Specific Wi-Fi Adapter

If your laptop has both an internal Wi-Fi card and a USB Wi-Fi dongle, you must specify which adapter should handle the connection. Without the interface parameter, Windows uses whichever adapter it considers primary.

@echo off
setlocal enabledelayedexpansion

set "Adapter=Wi-Fi 2"
set "Target=Guest_Network"

rem --- Verify the Wireless AutoConfig service is running ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Wireless AutoConfig service is not running.
endlocal
pause
exit /b 1
)

rem --- Verify the specified adapter exists ---
netsh wlan show interfaces 2>nul | findstr /i /c:"%Adapter%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Adapter "%Adapter%" not found.
echo [INFO] Available wireless interfaces:
netsh wlan show interfaces | findstr /i /c:"Name"
endlocal
pause
exit /b 1
)

rem --- Verify the profile exists ---
netsh wlan show profiles 2>nul | findstr /i /c:"%Target%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] No saved profile found for "%Target%".
endlocal
pause
exit /b 1
)

echo [ACTION] Connecting to "%Target%" on adapter "%Adapter%"...

netsh wlan connect name="%Target%" interface="%Adapter%"

if %errorlevel% neq 0 (
echo [ERROR] Connection command failed.
endlocal
pause
exit /b 1
)

echo [INFO] Connection command sent. Waiting for handshake...
timeout /t 8 /nobreak >nul

rem --- Verify connection on the specified interface ---
set "Connected=false"
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 (
set "Value=%%b"
if defined Value (
set "Value=!Value:~1!"
if /i "!Value!"=="%Target%" set "Connected=true"
)
)
)

if "!Connected!"=="true" (
echo [SUCCESS] Connected to "%Target%" on "%Adapter%".
) else (
echo [WARN] Connection may not have completed. Verify manually.
)

endlocal
pause

Finding adapter names: Run netsh wlan show interfaces to see the exact Name field for each wireless adapter. Common names include Wi-Fi, Wi-Fi 2, Wireless Network Connection, or custom names set by the user.

Method 3: Auto-Reconnect Monitor

For remote kiosks, data loggers, or digital signage that must stay online, this script continuously monitors the connection and reconnects if it drops. The original version had a flawed SSID check that could match BSSID lines or produce false positives.

@echo off
setlocal enabledelayedexpansion

set "SSID=Kiosk_WiFi"
set "CheckInterval=60"
set "ReconnectWait=10"
set "LogFile=%~dp0wifi_monitor.log"

rem --- Verify the Wireless AutoConfig service is running ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Wireless AutoConfig service is not running.
endlocal
pause
exit /b 1
)

rem --- Verify the profile exists ---
netsh wlan show profiles 2>nul | findstr /i /c:"%SSID%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] No saved profile found for "%SSID%".
endlocal
pause
exit /b 1
)

echo [MONITOR] Watching connection to "%SSID%"...
echo [MONITOR] Check interval: %CheckInterval% seconds
echo [MONITOR] Log file: %LogFile%
echo [MONITOR] Press Ctrl+C to stop.
echo.

:Monitor
rem --- Extract the current SSID (excluding BSSID lines) ---
set "CurrentSSID="
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 the current SSID against the target ---
if /i "!CurrentSSID!"=="%SSID%" (
echo [!time!] OK - Connected to "%SSID%"
) else (
if defined CurrentSSID (
echo [!time!] ALERT - Connected to "!CurrentSSID!" instead of "%SSID%". Reconnecting...
echo !date! !time! - RECONNECT - Was on "!CurrentSSID!" >> "%LogFile%"
) else (
echo [!time!] ALERT - No Wi-Fi connection. Reconnecting...
echo !date! !time! - RECONNECT - Disconnected >> "%LogFile%"
)

netsh wlan connect name="%SSID%"

if !errorlevel! neq 0 (
echo [!time!] ERROR - Reconnect command failed.
echo !date! !time! - ERROR - Connect command failed >> "%LogFile%"
) else (
echo [!time!] INFO - Reconnect command sent. Waiting %ReconnectWait%s...
timeout /t %ReconnectWait% /nobreak >nul

rem --- Verify reconnection ---
set "VerifySSID="
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 VerifySSID (
set "VerifySSID=%%b"
if defined VerifySSID set "VerifySSID=!VerifySSID:~1!"
)
)
)

if /i "!VerifySSID!"=="%SSID%" (
echo [!time!] SUCCESS - Reconnected to "%SSID%".
echo !date! !time! - SUCCESS - Reconnected >> "%LogFile%"
) else (
echo [!time!] WARN - Reconnect may have failed. Will retry next cycle.
echo !date! !time! - WARN - Reconnect unverified >> "%LogFile%"
)
)
)

timeout /t %CheckInterval% /nobreak >nul
goto :Monitor

How to Avoid Common Errors

Wrong Way: Trying to Pass a Password Directly

The netsh wlan connect command does not accept a password argument. It only references saved profiles.

rem *** BAD: there is no password parameter ***
netsh wlan connect name="MyNetwork" password="secret123"

Correct Way: Connect manually once to save the profile, or import a profile XML file.

rem --- Import a pre-built profile XML ---
netsh wlan add profile filename="MyNetwork.xml"
rem --- Then connect using the profile ---
netsh wlan connect name="MyNetwork"

Wrong Way: Running a Network Command Immediately After Connecting

The netsh wlan connect command returns immediately after sending the connection request. The actual Wi-Fi handshake and DHCP IP assignment take several seconds. Running a network command too early will fail.

rem *** BAD: ping runs before the connection is established ***
netsh wlan connect name="MyNetwork"
ping server.example.com

Correct Way: Wait for the handshake and DHCP to complete, then verify.

netsh wlan connect name="MyNetwork"
timeout /t 8 /nobreak >nul
ping -n 1 server.example.com >nul 2>&1
if %errorlevel% equ 0 echo [OK] Network is reachable.

Wrong Way: Assuming Profile Name Equals SSID

The profile name in Windows is usually identical to the SSID, but they can differ: for example, if the SSID was renamed on the router after the profile was saved, or if the profile was imported with a custom name.

rem *** RISKY: profile name might not match the current SSID ***
netsh wlan connect name="OldNetworkName"

Correct Way: Run netsh wlan show profiles to see the exact profile names stored in Windows.

rem --- List all saved profiles ---
netsh wlan show profiles

Problem: Windows Switches Back to a "Preferred" Network

If multiple saved networks are visible and another network has higher priority or "Connect automatically" enabled, Windows may override your scripted connection within seconds.

Solution: Disable auto-connect on competing networks, or set your target network to highest priority:

rem --- Set the target profile as the highest-priority connection ---
netsh wlan set profileorder name="Office_WiFi" interface="Wi-Fi" priority=1

Best Practices and Rules

1. Always Verify After Connecting

A successful errorlevel 0 from netsh wlan connect means the command was accepted, not that the connection succeeded. Always wait a few seconds and then check netsh wlan show interfaces to confirm the actual SSID.

2. Wait for IP Assignment

Wi-Fi connection involves multiple steps: authentication, association, and DHCP. Allow at least 5–10 seconds before running any network-dependent commands. For enterprise networks with 802.1X authentication, you may need 15–20 seconds.

3. Set Network Priority

If you have multiple saved networks visible in the same location, use netsh wlan set profileorder to control which one Windows prefers. This prevents Windows from fighting your script by switching to a different network.

4. Log Connection Events

In automated environments (kiosks, data loggers, digital signage), log every connection and reconnection event. Frequent reconnections indicate a hardware problem, signal interference, or an overloaded access point, not a script issue.

5. Use setlocal / endlocal

Always wrap scripts in setlocal and endlocal to prevent variables from persisting beyond the script's execution.

6. Handle the No-Wi-Fi Case

If the machine has no wireless adapter or the adapter is disabled, netsh wlan commands will fail. Check for the WlanSvc service at the start of your script and exit gracefully with a clear message.

Conclusions

Connecting to a Wi-Fi network via Batch script gives you direct control over your machine's wireless behavior. The critical points are: the connection only works with saved profiles (not raw passwords), the errorlevel only confirms the command was sent (not that it succeeded), and Windows may override your connection if another network has higher priority. By verifying profiles before connecting, waiting for the handshake, confirming the actual SSID afterward, and logging every event, you transform a simple one-line netsh wlan connect into a robust, production-ready automation tool for kiosks, mobile deployments, and location-aware systems.