Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

after I make a VPN connection to my work/whatever, I currently have to go into the command prompt and manually add a route.


ROUTE ADD METRIC 1 or whatever the command is.

Is it possible to have this automatically happen after I successfully make a VPN connection?

share|improve this question
up vote 2 down vote accepted

If you want to make it a 1-step process, you could create a batch file that runs rasdial to automate your VPN connection and then does a ROUTE ADD:

rasdial "connection name" username password ('*' to prompt for password)

This assumes you're connecting to a Microsoft VPN, but you could script the OpenVPN client in the same way:

openvpn c:\path\to\config-file.ovpn
share|improve this answer
OpenVPN client should automatically add the appropriate routes, assuming it is configured correctly. – MDMarra Sep 5 '10 at 6:25
True for the route(s) assigned/pushed from the server, but there may be additional routes you want to define. – nedm Sep 5 '10 at 6:33
OOO! now that's interesting :) Was a typo in the ROUTE ADD line .. but besides that the .bat doesn't work cause it needs to run in privileged mode :( – Pure.Krome Sep 5 '10 at 13:14
Yes, 'route add' requires admin privileges, but you should be able to right-click on the command prompt taskbar icon and choose 'Run As Different User' and enter admin credentials, then run the bat file from the command line. I would place the .bat file in whichever directory that doing so leaves the command prompt in by default, so you can just type 'vpnscript.bat' or whatever after login without switching directories. Alternatively, control-shift-click should also open the command prompt in privileged/admin mode w/ UAC. – nedm Sep 5 '10 at 20:38

You could make the route persistent (I think with route -p) so you don't need to enter it each time. If you are using openVPN, the server can send a route to the client: push "route" for example. With other VPNs servers I dont' know but I guess they may have a similar option too.

share|improve this answer
so it cannot be made, client side .. and on demand? – Pure.Krome Sep 5 '10 at 3:29
I don't know any way to do it (with openVPN) on client side and not permanent different from nedm's answer but there will be the need to run the bat file with admin privilege. I think a persistent route is not a bad solution if you can't modify the openVPN server config. – laurent Sep 6 '10 at 1:43

If you have multiple VPNs you might run into the issue that when they connect in random order, their interface IDs change. In that case the normal ROUTE -P ADD MASK IF 42 does not work. The next time the VPN connects it might have a different interface number.

Powershell has a cmdlet available that adds routes on VPN connection and removes them again when the VPN is disconnected: Add-VpnConnectionRoute. It works without having to specify the interface ID.

The basic syntax is like this:

Add-VpnConnectionRoute -ConnectionName "VPN Connection Name" -DestinationPrefix

After entering this command, the routes will be created/removed automatically on connection/disconnection of the VPN.

share|improve this answer
netsh interface ipv4 add route [destination/prefixlength] "[interface/connection name]"

I'm using that to deal with connections that have subnet overlap by adding static routes for hosts on the remote subnet - servers and the like.

share|improve this answer
This is exactly what I needed as I am using Windows Server 2008. The PoswerShell solution did not work. – Michael Coxon May 17 at 3:20

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.