Is there a simple way of doing this via a shell command / batch ? A VBScript would also be ok.
4 Answers
To clear all routes, use:
route -f
To clear only persistent routes, you could use
reg delete HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\PersistentRoutes /va /fbut this wouldn't remove them from the currently active routes.
To delete all IPv4 persistent routes except the default route :
for /f "skip=3 tokens=4,6" %e in ('netsh int ipv4 sh route store^=persistent ^| findstr -v 0.0.0.0/0') do route delete -4 -p %e %f
To delete all IPv6 persistent routes except the default route :
for /f "skip=3 tokens=4,6" %e in ('netsh int ipv6 sh route store^=persistent ^| findstr -v ::/0') do route delete -6 -p %e %f
Here is a cmd-script that extracts the static routes from the registry, and issues route delete
commands for each of them.
This method removes them both from the stored list of persistent routes, and from the currently active routes.
It also deletes the default route if it's found in the registry :
@echo off
set key=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\PersistentRoutes
for /f "tokens=1,2,3,* delims=," %%i in ('reg query %key% ^| find "REG_SZ"') do (
route delete %%i mask %%j %%k
)
To prevent removing the default route, you can type this :
@echo off
set key=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\PersistentRoutes
for /f "tokens=1,2,3,* delims=," %%i in ('reg query %key% ^| find /v "0.0.0.0,0.0.0.0" ^| find "REG_SZ"') do (
route delete %%i mask %%j %%k
)
This can be done as a basic for-loop allowing you to clear the routes correctly for the existing and future routes.
( And without getting involved in reviewing or editing any registry items) using a simple route print piped into a find and looped into a route delete.
FOR /F "TOKENS=1-5" %a IN ('ROUTE PRINT ^| FIND /I /V "On-Link" ^| FINDSTR /E /R /C:" *\.[0-9]*[0-9] *[0-9][0-9]*"') DO @(IF /I "%~e" EQU "" ( ROUTE DELETE %a MASK %b %c ) )