Skip to main content

How to Add a Persistent Static Route in Batch Script

In complex network environments, the default gateway isn't always enough. You might need to tell your computer to send traffic for a specific office subnet through a different VPN gateway or a dedicated fiber link. A "Static Route" is an instruction that overrides the default path for specific destinations. By adding a "Persistent" route via Batch script, you ensure that these custom networking rules survive a reboot, making them a permanent part of your machine's configuration.

This guide will explain how to manage static routes using the route command.

Method 1: Adding a Persistent Route

The -p flag is the key to persistence. Without it, the route will disappear the next time the computer restarts.

@echo off
setlocal

set "TargetNetwork=10.0.5.0"
set "SubnetMask=255.255.255.0"
set "Gateway=192.168.1.254"

echo [NET] Attempting to add persistent route...
echo [INFO] Destination: %TargetNetwork% mask %SubnetMask% via %Gateway%

rem --- Verify the gateway is reachable before adding the route ---
ping -n 1 -w 1000 %Gateway% >nul 2>&1
if %errorlevel% neq 0 (
echo [WARN] Gateway %Gateway% is not responding to ping.
echo [WARN] The route will be added but may be unreachable.
)

rem --- Delete any existing route to prevent "already exists" errors ---
route delete %TargetNetwork% >nul 2>&1

rem --- Add the persistent route ---
rem -p = Persistent (survives reboot)
rem add = Operation
rem %TargetNetwork% = Destination network
rem mask = Keyword (required before the subnet mask value)
rem %SubnetMask% = Defines which portion of the destination is the network
rem %Gateway% = Next-hop router address
route -p add %TargetNetwork% mask %SubnetMask% %Gateway%

if %errorlevel% equ 0 (
echo [SUCCESS] Route added permanently.
) else (
echo [ERROR] Failed to add route. Ensure this script is running as Administrator.
)

endlocal
pause
warning

Administrative Rights Required. Modifying the routing table is a significant system change. You MUST run your Batch script as an Administrator; otherwise, the route add command will fail with an "Access Denied" error. The route delete cleanup step will also fail silently without elevation.

Method 2: Verifying the Route

After adding a route, you should always verify that Windows has accepted it correctly. findstr returns errorlevel 0 when it finds a match, but route print itself always returns 0 regardless: so the pipeline works correctly only if findstr is the last command whose exit code is evaluated.

@echo off
setlocal

set "TargetNetwork=10.0.5.0"

echo [AUDIT] Checking routing table for %TargetNetwork%...

rem --- Capture the findstr result explicitly ---
route print | findstr /c:"%TargetNetwork%" >nul 2>&1

if %errorlevel% equ 0 (
echo [OK] Route for %TargetNetwork% is active:
echo.
route print | findstr /c:"%TargetNetwork%"
) else (
echo [FAIL] Route for %TargetNetwork% not found in routing table.
)

endlocal
pause

Method 3: Removing a Route

If you no longer need the custom path, you can delete it. If the route didn't exist, the user received no feedback.

@echo off
setlocal

set "TargetNetwork=10.0.5.0"

echo [NET] Removing custom route for %TargetNetwork%...

rem --- Check if the route exists before attempting removal ---
route print | findstr /c:"%TargetNetwork%" >nul 2>&1

if %errorlevel% neq 0 (
echo [INFO] Route for %TargetNetwork% does not exist. Nothing to remove.
goto :End
)

route delete %TargetNetwork%

if %errorlevel% equ 0 (
echo [SUCCESS] Route for %TargetNetwork% removed.
) else (
echo [ERROR] Failed to remove route. Ensure this script is running as Administrator.
)

:End
endlocal
pause

How to Avoid Common Errors

Wrong Way: Forgetting the mask Keyword

If you try to add a route like route add 10.0.5.0 192.168.1.1 without the mask keyword, Windows interprets the second address as the gateway and assumes a default mask, which is almost never what you intended.

rem *** BAD - no mask keyword; Windows guesses the mask ***
route add 10.0.5.0 192.168.1.1

Correct Way: Always specify the mask keyword and value explicitly so the boundaries of the target network are unambiguous.

rem *** GOOD - mask is explicit ***
route -p add 10.0.5.0 mask 255.255.255.0 192.168.1.254

Wrong Way: Not Checking for Administrator Privileges

The route command fails silently or with a cryptic error when run without elevation. The script should detect this early.

rem --- Quick admin check: attempt to read a protected location ---
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
pause
exit /b 1
)

Problem: Invalid or Unreachable Gateway

If you specify a gateway address that the computer cannot reach (e.g., it is on a different subnet with no existing path), the route will be added but all traffic sent through it will be undeliverable.

Solution: Ping the gateway before adding the route (as shown in Method 1) and warn the user if it does not respond.

Best Practices and Rules

1. Manual Metric

If you have two paths to the same network, use the METRIC parameter to tell Windows which one to prefer. A lower metric means higher priority.

route -p add 10.0.5.0 mask 255.255.255.0 192.168.1.254 metric 10

2. Interface Index

On machines with many network adapters (Wi-Fi, Ethernet, VPN), you can force the route to use a specific adapter using the IF (Interface) parameter. Find the correct index with route print and look at the "Interface List" section at the top.

route -p add 10.0.5.0 mask 255.255.255.0 192.168.1.254 if 5

3. Delete Before Adding

To prevent "The route addition failed: The object already exists" errors, delete the route first. Redirect errors to nul so a missing route doesn't produce noise.

route delete 10.0.5.0 >nul 2>&1
route -p add 10.0.5.0 mask 255.255.255.0 192.168.1.254

4. Always Verify After Adding

A successful route add exit code does not guarantee the route is functional. Always follow up with route print | findstr to confirm the route appears in the active table.

5. Use setlocal / endlocal

Wrap your scripts in setlocal and endlocal to prevent variables like TargetNetwork or Gateway from leaking into the parent environment.

Full Production Script

This combined script handles adding, verifying, and optionally removing a persistent static route with all safeguards in place.

@echo off
setlocal

rem ==============================
rem Configuration
rem ==============================
set "TargetNetwork=10.0.5.0"
set "SubnetMask=255.255.255.0"
set "Gateway=192.168.1.254"

rem ==============================
rem Admin Check
rem ==============================
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
pause
exit /b 1
)

rem ==============================
rem Gateway Reachability Check
rem ==============================
echo [NET] Pinging gateway %Gateway%...
ping -n 1 -w 1000 %Gateway% >nul 2>&1
if %errorlevel% neq 0 (
echo [WARN] Gateway %Gateway% did not respond. Route may be unreachable.
) else (
echo [OK] Gateway %Gateway% is reachable.
)

rem ==============================
rem Add Persistent Route
rem ==============================
echo [NET] Adding persistent route...
route delete %TargetNetwork% >nul 2>&1
route -p add %TargetNetwork% mask %SubnetMask% %Gateway%

if %errorlevel% neq 0 (
echo [ERROR] Failed to add route.
pause
exit /b 1
)

echo [SUCCESS] Route added.

rem ==============================
rem Verify
rem ==============================
echo [AUDIT] Verifying routing table...
route print | findstr /c:"%TargetNetwork%" >nul 2>&1

if %errorlevel% equ 0 (
echo [OK] Route confirmed in routing table:
route print | findstr /c:"%TargetNetwork%"
) else (
echo [WARN] Route was accepted but not found in table. Investigate manually.
)

endlocal
pause

Conclusions

Adding persistent static routes via Batch script is a powerful way to manage complex network traffic.

The -p flag ensures your routing rules survive reboots, but a robust script must also check for administrative privileges, verify gateway reachability, clean up existing routes before adding new ones, and confirm the result.

These safeguards transform a one-line route add command into a reliable, production-ready automation tool for VPN management, multi-site networking, and distributed office connectivity.