New NetRoute - PowerShell
Overview
New-NetRoute
creates or modifies a static route in the routing table. It allows administrators to specify a destination prefix, next hop address, metric, and other parameters to control network traffic flow.
Syntax
New-NetRoute -DestinationPrefix <IPAddress/PrefixLength> -NextHop <IPAddress/Gateway> [-RouteMetric <Integer>]
[-InterfaceIndex <Integer>] [-Persistent <Boolean>] [-PassThru] [-Confirm] [-WhatIf]
Options/Flags
- DestinationPrefix: Specifies the destination network prefix (IPv4 or IPv6) and prefix length to which the route applies.
- NextHop: Specifies the IP address of the next hop router to which traffic is forwarded for the specified destination prefix.
- RouteMetric: Sets the route metric for the new route. A lower metric indicates a more preferred route. (Default: 100)
- InterfaceIndex: Specifies the interface index of the network interface through which traffic should be forwarded for the route.
- Persistent: Sets the persistence of the route. A persistent route is saved to the routing table configuration and survives reboots. (Default: $false)
- PassThru: Returns the newly created route object.
- Confirm: Prompts the user to confirm the operation.
- WhatIf: Shows what the command would do without actually performing the action.
Examples
Create a persistent route with a metric of 200:
New-NetRoute -DestinationPrefix 192.168.1.0/24 -NextHop 192.168.1.1 -RouteMetric 200 -Persistent
Create a temporary route for a specific interface:
New-NetRoute -DestinationPrefix 10.0.0.0/24 -NextHop 10.0.0.1 -InterfaceIndex 12
Retrieve the newly created route object:
$newRoute = New-NetRoute -DestinationPrefix 192.168.2.0/24 -NextHop 192.168.2.1 -PassThru
Common Issues
- Route already exists: If a route for the specified destination prefix already exists, the command will fail with an error. Use
Get-NetRoute
to check for existing routes. - Invalid prefix or next hop: Ensure that the specified destination prefix and next hop address are valid IP addresses or gateways.
Integration
New-NetRoute
can be combined with other PowerShell commands to automate network configuration tasks. For example, it can be used with Get-NetRoute
to retrieve existing routes and Remove-NetRoute
to delete them.
Related Commands
Get-NetRoute
Remove-NetRoute
Set-NetRoute
netsh