Computer >> Computer tutorials >  >> System >> Windows Server

Automatically Add Static Routes After Connecting to VPN

In the latest Windows 10 builds, you can automatically add static routes when establishing a VPN connection. When the VPN connection is terminated, the route is automatically removed from the Windows routing table. To add an IPv4 or IPv6 route for a VPN connection, the Add-VpnConnectionRoute PowerShell cmdlet is used.

Of course, you can manually add routes for your VPN connections via the command prompt, but they will be cleared after disconnecting from the VPN. So the next time you connect to VPN, you will have to manually add routes again.

Suppose, you want only traffic of two subnets (192.168.11.0/24 and 10.1.0.0/16) to be routed through your VPN connection, and other traffic to go through your provider (ISP).

Open the PowerShell console and display the list of configured VPN connections in Windows:

Get-VpnConnection

Automatically Add Static Routes After Connecting to VPN

First of all, uncheck the Use default gateway in the remote network option. You can do it in the VPN connection properties in the Control Panel or using the command below:

Set-VpnConnection –Name workVPN -SplitTunneling $True

Learn more about SplitTunneling in the article No Internet Connection After Connecting to VPN.

Automatically Add Static Routes After Connecting to VPN

Let’s add two static routes for our VPN connection:

Add-VpnConnectionRoute -ConnectionName workVPN -DestinationPrefix 192.168.11.0/24 –PassThru
Add-VpnConnectionRoute -ConnectionName workVPN -DestinationPrefix 10.1.0.0/16 –PassThru

In the DestinationPrefix option, specify a subnet or a host IP address you want to route traffic to through the VPN. To add a single host by IP address, use the following format: 10.1.1.26/32.

Automatically Add Static Routes After Connecting to VPN

DestinationPrefix : 192.168.11.0/24
InterfaceIndex :
InterfaceAlias : workVPN
AddressFamily : IPv4
NextHop : 0.0.0.0
Publish : 0
RouteMetric : 1

If your VPN connection is active, you will need to reconnect so that new routes are added to the routing table.

Automatically Add Static Routes After Connecting to VPN

New routes are bound to the VPN connection and are added only when the connection is established. When you disconnect from the VPN server, the routes are automatically removed.

Disconnect from the VPN and check the routing table. The route to your remote network has been removed automatically, and Get-NetRoute returns that the route was not found:

Get-NetRoute : No MSFT_NetRoute objects found with property 'DestinationPrefix' equal to '192.168.11.0/24'. Verify the value of the property and retry. CmdletizationQuery_NotFound_DestinationPrefix,Get-NetRoute

Automatically Add Static Routes After Connecting to VPN

To completely remove a static route for a VPN connection, use the command:
Remove-VpnConnectionRoute -ConnectionName workVPN -DestinationPrefix 192.168.111.0/24 -PassThru

If you want to change the order of DNS name resolution with an active VPN connection, read this article.

In previous Windows versions (Windows 7/ Windows Server 2008R2), to dynamically add routes after establishing a VPN connection, you had to use the CMAK and various scripts with add route commands.

For example, you can create a batch file vpn_route.netsh to add some static routes.

interface ipv4
add route prefix=192.168.11.24 interface="workVPN" store=active
add route prefix=10.1.0.0/16 interface="workVPN" store=active
exit

You can run this file using the Task Scheduler that triggers after the VPN connection is established (the RasMan 20225 event in the Event Viewer).

schtasks /create /F /TN "Add VPN routes" /TR "netsh -f C:\PS\vpn_route.netsh" /SC ONEVENT /EC Application /RL HIGHEST /MO "*[System[(Level=4 or Level=0) and (EventID=20225)]] and *[EventData[Data='My VPN']]"