Our website relies on funding from our readers, and we may receive a commission when you make a purchase through the links on our site.

How to add a static route using a PowerShell cmdlet

by John Cirelly - Last Updated: July 24, 2023

How to add a static route using a PowerShell cmdlet

Since static routes do not take part in any kind of automatic discovery process, it is very easy to manage and configure them. A list of potential destinations can be generated using a static route by combining the network address of the destination with the subnet mask. Static routes are very straightforward. The Windows operating system comes equipped with an internal routing table that gives it the ability to select appropriate paths for data transmission.

The process of taking a packet from one device and transmitting it through the network to another device that is connected to a different network is referred to as routing. When sending packets through a routed network, the first step is to use the logical network address of the host that the packets are destined for. After that, the hardware address of the host, also known as the MAC address, is used to ensure that the packet is sent to the correct destination host.

What is a routing device and how does the process of routing work?

The routing device acquires knowledge of remote networks from other routing devices in the vicinity of the administrator of the network. After that, the routing device constructs a routing table, which is essentially a map of the network that details how to locate the remote networks. If a network is directly connected, the router or other device that routes traffic already has the information necessary to reach that network.

When a data packet is received by a network, the router immediately begins to read the headers, which include information regarding the packet’s ultimate destination. After that, it compares the information with the entries in the routing table (which is a list of objectives) and chooses which route is the most efficient to take to go to the desired location. In the end, the router will propose a route for the packets to take, which will involve passing through several other routers on the way to the destination node.

Categories of routing

Static routing, dynamic routing, and default routing are the three distinct categories of routing operations. Because only the network administrator may manually set up or update any modifications, altering the static routing is a process that is not particularly user-friendly. On the other hand, dynamic routing makes use of a wide variety of routing protocols and automatically updates itself. Static routing is preferred over dynamic routing by most small networks because it does not require as much computer power as dynamic routing does. Nevertheless, any network of medium or big size may count the dynamic routing method as an option because it is more effective.

Why should we include Static Routes?

The vast majority of routers, including the one that is already built into your windows computer, implement dynamic routing in some form or another. However, there are times when static routing may be employed; in such cases, the network administrator is responsible for manually updating all modifications and adding static routes to the routing device. The following are some scenarios in which you could find it necessary to construct static routes:

Security concerns

Because security concerns prevent some routes from being added to the default gateway device, you will need to make use of an additional routing device to carry out certain operations. In such a scenario, it is possible to configure a nearby Windows machine so that it can carry out the necessary routing functions.

VLANs or subnets

On your network, you have created many subnets or VLANs, and now you need to route traffic to one of those subnets in particular. Testing these kinds of setups can be made significantly easier with the utilization of static routes.

Trafficking purposes

You want to use the secondary internet router on the network to send traffic for social media and audio/video streaming services like Facebook, Instagram, Twitter, YouTube, Netflix, and Spotify while using the primary internet router to send traffic for all other services. You have two internet routers on the network: one is primary and the other is secondary.

You want to have better control of the routing table and the flow of traffic coming into and going out of your network. You are currently utilizing a Windows PC as a router for your network.

What is meant by the term “Routing Table”?

Before a device on a network may transfer data to another device on the same network, the device must first determine where the data should be sent. If the network device is unable to establish a direct connection with the device to which it is sending data, the data will be relayed to the destination device via a series of intermediary devices. Each device needs to keep track of which direction to transport different packets of data, and to do so, it makes use of something that we refer to as a routing table.

When it comes to the delivery of packages, a routing table is comparable to a distribution map. It is a database that, like a map, keeps track of paths and uses these to decide when traffic leaves a routing device, whether that device is a real router or a personal computer. It does this by using the paths to determine which way to forward traffic. Before making any modifications to your existing routing table, it is vital to first review the table in its current state. To access the routing table, please follow the procedures below in order:

  1. In the search bar provided by Windows, type “powershell” to locate the PowerShell application.
  2. Select the application with the right mouse button, then click “Run as administrator”.
  3. Once the PowerShell command prompt has opened, type the following command to inspect the routing table: route print.

What PowerShell cmdlet can be used to add a static route?

Managing static routes in the routing table is the responsibility of the ip-route command. It is necessary to issue this command for each static route that you wish to add to the routing database. Use the no ip-route command if you want to get rid of a static route. Execute this command for every static route that you want to remove from the routing table.

The Step-by-Step Guide to Adding a Static Route Using PowerShell CMDLETs

The following is an example of the fundamental syntax required to add a static route in Windows by using a PowerShell CMDLET:

route /f /p mask metric if the condition is met

The following provides a concise explanation of each available choice:

  1. /f is a command that can be used to clear the routing table.
  2. Using the /p switch will add the routing table to the network permanently. The command is a notation that is utilized to specify the command that
  3. you wish to use, such as add, change, or delete.
  4. /p add as the route 192.168.0.0 mask 255.255.0.0 192.168.0.1 if 0x2

How exactly does one go about including a Static Route in the Windows Routing Table?

The following is the syntax that must be used to add a static route to a routing table in a routing device that is based on Windows:

route [/f] [/p] [<command> [<destination>] [mask <netmask>] [<gateway>] [metric <metric>]] if (‘interface’), then (‘if’)

The syntax for adding a static route includes the following important parameters, which are discussed below:

  • The -f switch erases any entries in the routing table that do not host routes (routes that have a netmask of 255.255.255.255) as well as the loopback network route.
  • -p When used in conjunction with the add command, the route that you specify will be added to the registry. This route will then be used to initialize the IP routing table each time the TCP/IP protocol is started.
  • Command: Identifies the command that will be carried out. The following are examples of valid commands: (add, change, delete, print)
  • Destination: Identifies the network location where the route will ultimately end up.
  • Mask: the network destination’s netmask (also known as the subnet mask) is indicated by the netmask parameter.
  • Gateway: Identifies the IP address of the next hop, also known as the forwarding IP address, over which the group of addresses that are determined by the network destination and the subnet mask can be accessed.
  • Metric: Specifies an integer cost metric for the route, which can range anywhere from one to nine thousand and nine hundred ninety-nine. This metric is used to choose from several routes in the routing table that most closely match the destination address of a packet that is being forwarded. The path that results in the lowest metric is the one that is selected.
  • Interface: Indicates the index of the interface that the destination can be accessed through.

Follow the steps below to enter a static route to the destination 10.51.0.0 in PowerShell using the syntax described above. The subnet mask should be set to 255.255.0.0, and the following hop address should be 10.23.0.1. This will serve as a working example of the syntax described above.

  1. In the search bar provided by Windows, type “powershell” to locate the PowerShell application.
  2. Select the application with your right mouse button, and then select “Run as administrator”.
  3. When the PowerShell command prompt appears, at this point you should type the following command to add the static route.

route add 10.51.0.0 mask 255.255.0.0 10.23.0.1

Static route with persistence

As was mentioned earlier, this route will only remain in the routing table until Windows is restarted. Static routes with persistence are recommended. When that takes place, the information included in the routing table will be wiped clean. To make the entry permanent, you will need to add the -p switch to the command that was just described. Type the following command, for instance, to add a permanent route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0 and the next hop address of 10.23.0.1:

/p add as the route 10.51.0.0 mask 255.255.0.0 10.23.0.1

Type the following command to add a static route with a metric to the destination 10.51.0.0 with the subnet mask of 255.255.0.0, the next hop address of 10.23.0.1, and the cost metric of 65:

route added with 10.51.0.0, masked with 255.255.0.0, and the metric set to 65

The route that is permanently set up with an interface

When viewing the routing table using the route print command, the interface list numbers are presented shortly before the IPv4 route table. Loopback is represented by the index number 1 (0x1), and the remaining indices are distributed among the various existing network interfaces.

Type the following command, for instance, to add a route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0, the following hop address of 10.23.0.1, and using the interface index 0x7:

route add 10.51.0.0 mask 255.255.0.0 10.23.0.1 if 0x7

Conclusion

This post explained how to use PowerShell to add a static route to a routing table along with other important parameters. If you use the route print command to view the routing table, you’ll be able to see all of your static route entries, which is useful if you want to review the contents of the entries you’ve created.

We hope that at the end of this article, you will have a better understanding of the notion of a static route as well as how to build it in operating systems that use the Windows platform.

Add Static Route Using PowerShell cmdlet FAQs

What is the PowerShell cmdlet used to add a static route?

The PowerShell cmdlet used to add a static route is New-NetRoute.

What are the parameters for the New-NetRoute cmdlet?

The New-NetRoute cmdlet takes several parameters, including DestinationPrefix (the destination IP address and subnet mask), NextHop (the IP address of the next hop router), and InterfaceIndex (the index of the network interface to use).

What are some common use cases for adding a static route using PowerShell?

Some common use cases for adding a static route using PowerShell include configuring VPN connections, routing traffic between subnets, and configuring network load balancing.

What permissions are required to add a static route using PowerShell?

Adding a static route using PowerShell requires administrator-level permissions.

How can I verify that the static route has been added successfully?

You can use the Get-NetRoute cmdlet to verify that the static route has been added successfully. Alternatively, you can use the route print command in the Windows command prompt to view the routing table.

Is it possible to remove a static route using PowerShell?

Yes, you can use the Remove-NetRoute cmdlet to remove a static route that has been added using PowerShell.

Can I add multiple static routes at once using PowerShell?

Yes, you can add multiple static routes at once using PowerShell by using a loop or by specifying multiple DestinationPrefix and NextHop values in a single command.

Are there any limitations to adding static routes using PowerShell?

The New-NetRoute cmdlet only supports IPv4 routes. If you need to add an IPv6 static route, you will need to use the Add-NetRoute cmdlet instead. Additionally, some network adapters may not support all of the parameters of the New-NetRoute cmdlet, so it is important to verify that the adapter supports the required parameters before attempting to add a static route.