Add VpnConnectionRoute - PowerShell


Overview

The Add-VpnConnectionRoute command adds static routes to a VPN connection. These routes are used to direct traffic through the VPN tunnel instead of the default gateway.

Syntax

Add-VpnConnectionRoute [[-VpnConnectionName] <String>] [-DestinationPrefix <String>]

Required Parameters

| Parameter | Type | Description |
| — | — | — |
| VpnConnectionName | String | Name of the VPN connection to which the route should be added. |

Optional Parameters

| Parameter | Type | Description |
| — | — | — |
| DestinationPrefix | String | Destination prefix to which the route should be added. |

Options/Flags

None.

Examples

Example 1: Add a static route to a VPN connection

Add-VpnConnectionRoute -VpnConnectionName "MyVPNConnection" -DestinationPrefix "10.0.0.0/24"

This command adds a static route to the VPN connection named “MyVPNConnection” for the destination prefix “10.0.0.0/24.”

Example 2: Add multiple static routes to a VPN connection

$routes = @("10.0.0.0/24", "10.1.0.0/24", "10.2.0.0/24")

foreach ($route in $routes) {
    Add-VpnConnectionRoute -VpnConnectionName "MyVPNConnection" -DestinationPrefix $route
}

This command adds three static routes to the VPN connection named “MyVPNConnection” for the destination prefixes “10.0.0.0/24,” “10.1.0.0/24,” and “10.2.0.0/24.”

Common Issues

  • Error: The VPN connection does not exist.
    • Make sure that the specified VPN connection name exists.
  • Error: The destination prefix is not valid.
    • Ensure that the destination prefix is a valid IPv4 or IPv6 address prefix.

Integration

The Add-VpnConnectionRoute command can be used together with other PowerShell commands to automate VPN management tasks. For example, the following script creates a VPN connection and adds a static route to it:

$vpnConnection = New-VpnConnection -Name "MyVPNConnection" -Server "remote.server.com"
Add-VpnConnectionRoute -VpnConnectionName "MyVPNConnection" -DestinationPrefix "10.0.0.0/24"