Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can establish VPN connection using RASDIAL command. But how do I add a route to automatically after VPN is connected? Each time after VPN connection is established, Windows could assign different interface ID to the VPN connection (the IF parameter to the ROUTE command).

I can do that manually using the following method. ROUTE PRINT command shows a list of interfaces, I can write down an interface ID for the VPN (42 for example) and then issue a command

route ADD MASK IF 42

But how can I do this automatically in the .bat file?

I'm using PPTP VPN (it cannot automatically push routing to client).

Since interface ID is different each time VPN connects, I cannot use route -p (add a persistent route on clients). BTW, this is not a good solution, since VPN connection is sometimes used on office staff's notebooks. Sometimes their notebooks are part of the network, that is already a part of VPN and sometimes they establish VPN connection themselves.

This is a solution for PowerShell and it's ok for me. But I'm curious, can the same be done via simple .bat file?

share|improve this question
You can also try to use the option 121 of dhcp server to automatically push routes to clients? – user132796 Aug 20 '12 at 13:16
Yea, but I just think, are .bat files powerful enough to do the task... – Vladislav Rastrusny Aug 20 '12 at 14:04
up vote 2 down vote accepted

I had tried to find any good working solution too and failed and after that I decided to write it by myself. You can use it if you still need it.

share|improve this answer
Miraculous!!! ;) – Vladislav Rastrusny Jan 3 '13 at 16:31
I hope it's working very well for you. If you have any issues please inform me. Enjoy! – abdmob Jan 4 '13 at 17:07
@echo of
FOR /F "tokens=2 delims=:" %%a in ('IPCONFIG ^|FIND "IP" ^|FIND "192.168.99"') do set _IP=%%a 
set GW=%_IP:~1% 
echo %GW%
route add mask %GW% 
route add mask %GW% 
share|improve this answer
Please add some comments explaining what your script does and why it would help OP instead of just posting a bunch of code. – Frederik Nielsen Mar 27 at 20:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.