How to Add a Static Route Using a PowerShell CMDLET

How to Add a Static Route Using a PowerShell CMDLET

It is relatively simple to manage and set up static routes because they do not take part in any kind of autonomous route discovery procedure. By employing a static route and combining the network address of the destination with the subnet mask, it is possible to obtain a list of possible destinations to which the route could lead. Static routes are really simple to understand. The Windows operating system comes pre-installed with an internal routing table, which grants it the capacity to determine which paths are most suitable for data transfer and allows it to do so automatically.

In this post, we will just discuss static routing to understand how it operates. Additionally, adding a static route can be accomplished in a variety of methods; however, in this piece, we will discuss adding a static route by utilizing a PowerShell cmdlet.

What does Static Routing mean?

The process of routing is notoriously difficult since it requires sending each data packet over the right channels as it travels from its origin to its final destination. This is typically done based on a set of rules or protocols that determines the most optimal path for a data packet under different situations. This can be done based on a variety of different rules and protocols. A routing table is where you’ll find these rules written down.

What is meant by “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 uses something that we refer to as a routing table.

Regarding the delivery of packages, a routing table is similar 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 an actual router or a personal computer. It uses 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.

What exactly is contained within a Routing Table?

The IP address of the location to which a data packet will be delivered once it has been sent over a network.

  • Destination: The IP address to which a data packet is transferred over a network.
  • Next hop: The closest or following IP address to which the data packet is sent. The following hop address is calculated based on the routing protocol and its metrics.
  • Interface: This shows the local network interface that the device must follow to forward the packet from its origin to the next hop or the main gateway. The main gateway is the final destination for all packets.
  • Metric: It shows the costs of all possible routes, which is useful for finding the quickest and cheapest way to send data across a network.
  • Routes: Three types of routing tables are used to keep track of routes. There are three types of routing: dynamic routing, static routing, and default routing. The selection of a path that leads to the destination and has certain characteristics along the way is what we mean when we talk about a route.

How does the process of routing work?

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. Static routing, dynamic routing, and default routing are the three distinct categories of routing operations.

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. On the other hand, static routing may be utilized on occasion; in this case, it is the responsibility of the network administrator to manually update any modifications and add static routes to the routing device. The following are some scenarios in which you could find it necessary to construct static routes:

  • 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.
  • 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.

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

The Ip-route command is in charge of managing static routes in the routing table, and its responsibilities include these. Each static route that you want to add to the routing database requires that you issue this command on its own before you can do so. If you want to remove a static route, you can do so by using the no Ip-route command. Run this command for each static route that you want to take out of the routing database, and then exit the terminal.

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

To install a static route in Windows by utilizing a PowerShell CMDLET, the following is an example of the core syntax that is required:

Route if the condition is met, then the mask metric will be /f /p.

The following is a clear and brief description of every option that can be selected:

  1. The routing table can be cleared by using the command /f, which stands for “forward”.
  2. If you use the /p switch, the routing table will be added to the network in an irreversible way.
  3. The command is a notation that is used to specify the command that you wish to use, such as add, change, or delete. Examples of this kind of command include add, change, and delete.
  4. Insert /p after the route mask if 0x2

What steps must be taken to add a Static Route to the Windows Routing Table?

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

  • route [/f] [/p] [<command> [<destination>] [mask <netmask>] [<gateway>] [metric <metric>]] if the word “interface,” then the word “if”.

The syntax for creating a static route comprises several key arguments, which are broken down into the following categories and explored more below:

  • The -f switch removes from the routing table all entries that do not host routes (routes with a netmask of, in addition to the loopback network route.

When you run the add command with the -p switch in combination with it, the route that you specify will be added to the register. After that, this route will be utilized each time the TCP/IP protocol is launched to initialize the IP routing table.

  • Command: Identifies the command that will be carried out. The following are examples of valid commands: (add, change, delete, and print)
  • Destination: Indicates the area of the network where the route will eventually arrive at its final destination.
  • Mask: The netmask parameter provides information on the network destination’s netmask, which is also referred to as the subnet mask.
  • Gateway: Identifies the IP address of the next hop, which is also known as the forwarding IP address. This address is used to gain access to the set of addresses that are determined by the network destination and the subnet mask.
  • Metric: Specifies an integer cost metric for the route, which can range anywhere from one to nine thousand nine hundred ninety-nine. This value can be used to calculate the total cost of the route. This measure is utilized to select, from among the several routes contained in the routing table, the one that provides the best possible approximation to the final destination address of a packet that is being relayed. The option that will bring the greatest reduction in metric value is the one that will be chosen.
  • Interface: Indicates the index of the interface through which the destination can be reached. Destination: Indicates the name of the destination.

To enter a static route in PowerShell with the syntax mentioned above, follow the steps that are provided below. The route will lead to destination It is recommended that the subnet mask be set to, and the address for the next hop should be The preceding explanation of syntax will now be illustrated by this functioning example.

  • To find the PowerShell application, open the Windows search bar and type “powershell”. This will take you to the appropriate location.
  • While holding down the right mouse button, select the application you want to run, and then select the option to “Run as administrator”.
  • When the PowerShell command prompt appears, you should now type the following command to add the static route at this point.

route add mask

Route with a static configuration and persistence

This route will only persist in the routing table until Windows is restarted, as was described before in this discussion. It is advised to use static routes that have persistence.

When this occurs, the information that was previously contained in the routing table will be removed completely. The command that was just explained needs to have the -p switch added to it for you to be able to make the entry permanent.

For example, to establish a permanent route to the destination with the subnet mask of and the next hop address of, type the following command: /p add as the route. This will populate the routing table with the appropriate information. mask

To add a static route with a metric to the destination, use the following command: The subnet mask should be set to, the next hop address should be set to, and the cost metric should be set to 65.

route with the address inserted, masked with the address, and metric set to 65

The path that has been permanently established with an interface.

When using the route print command to view the routing table, the interface list numbers are displayed momentarily before the IPv4 route table. The index number 1 (0x1) is used to represent the loopback, while the remaining indices are dispersed throughout the many network interfaces that are currently in use.

To add a route to the destination with the subnet mask of, the following hop address of, and using the interface index 0x7, for example, type the following command:

route add mask if 0x7

Final Words

This post walked through the process of using PowerShell to add a static route to a routing table, along with a few other crucial factors, and showed how to do it. If you want to evaluate the information that you’ve included in the static route entries that you’ve established, using the route print command to view the routing table allows you to see all of your static route entries. This is beneficial if you wish to do so.

We hope that by the time you conclude this post, you will not only have a better grasp of the concept of a static route but also of how to design one in operating systems that use the Windows platform.

Leave a Reply