header banner

Our funding comes from our readers, and we may earn a commission if you make a purchase through the links on our website.

How to add a static route using a Powershell CMDLET

How to add a static route using a Powershell CMDLET

Hitesh J UPDATED: February 7, 2023

What is Routing?

Routing is a process to choose a pathway to transfer data from one source to the final destination. The concept of routing is limited to computer networking, but it also covers other areas, including telephone networks, public transportation, etc. In Computer Networking, routing is performed by a device named router, under which a path is chosen for the data packets to travel across one or more networks. Under this concept, the routing protocols and costs help determine the best path to reach the final node.

Look at the picture to better understand how the routers choose a path to reach the destination. For example, there are two pathways in this image, Network (1,3 and 5) and Network (2 and 4), for the data packets to reach from source A to Destination B. Network 2 and 4 are the shortest, so the router may opt for it, but Network 1,3 and 5 might be faster. Thus, these are the challenges that a router faces and makes a choice accordingly.

what is routing

How Does Routing Work?

When a router receives a data packet, it immediately reads the headers stating its final destination. Then, it cross-checks the information with the routing table entries (a list of goals) and decides which path is best to reach the intended destination. Finally, the router recommends a pathway for the packets to travel, crossing various routers on their way to the final node. They are three types of routing processes – Static, Dynamic, and Default routing.  It is not easy to make changes in the static routing as only the network administrator can manually set up or update any changes. Dynamic Routing, on the other hand, use carious routing protocols and updates automatically. Also, it requires more computing power, which is why most small networks prefer static routing over dynamic routing. However, any medium and large-sized network may count the dynamic routing process as it is more efficient.

What is Routing Table?

The routing table or routing information base (RIB) rules work as a map to direct an efficient path for data packets to travel over an Internet Protocol network. In computer networking, routers and switches use these routing tables (stored in network host or router) to forward data packets across different computer networks.

The routing table comprises information about the arrangement of a communication network between devices, essential to forward a packet to its final destination. These work as a map or resource to help find the next-hop or best path to move a data packet. They use static and dynamic IP addresses to locate devices and accordingly direct the best pathway.

As a network device receives a data packet, it checks and matches with routing table entries (a list of destinations). These data packets keep track of where and which way to travel next to reach the final goal. Then, according to the match instructed by the routing table entry relating to the next hop, the packet moves to reach the final destination.

What’s included in a Routing Table?

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 routing protocol and its metrics.

Interface indicates the local network interface that the device must follow to forward the packet from its origin to the next hop or the main gateway.

Metric: It indicates the associated cost of each route, which further aids in determining the most cost-effective route or path for forwarding packets from one point to the other in a network.

Routes: Routing tables keep track of routes, classified into three types static routing, default routing, and dynamic routing.  A route is selecting a pathway connecting the destination and path attributes leading to the final destination.

One can maintain a routing table manually or dynamically. Only the network administrator can manually change the tables for static routing, and no bandwidth usage is required between the routers. On the other hand, automatic routing tables are built and maintained by the devices under routing protocols in dynamic routing. Also, dynamic routing is easy to configure and helps discover network destinations but is less secure than Static routing.

Why add Static Routes?

By default, the Windows operating system has a built-in routing table to make routing decisions. For example, you have a small office and don't want to purchase a hardware router; then you can use your existing Windows PC as a router by adding routing functions. There are two types of routing, dynamic routing, and static routing. Dynamic routing is configured automatically, while you will need to configure static routing manually.

You will need to add static routes in the following circumstances:

  • You have two internet connections, one for the internet network and the other for the work network, and want to route all traffic to a specific IP address range.
  • You can use your Windows PC as a router and want to control inbound and outbound traffic.
  • For example, you have multiple sub-network configured in your network and route all traffic to a particular subnet.

View the Windows Routing Table

Before adding a routing table, it is a good idea to view your current routing table first. Then, follow the below steps to view the routing table using the PowerShell CMDLET.

Step 1 – Search for PowerShell in the Windows search and locate the PowerShell.

Step 2 – Right-click on the PowerShell and click Run as administrator.

Step 3 – Once the PowerShell windows open, run the following command to view the routing table:

route print

You should see your routing table on the following screen:

display routing table

How to Add a Static Route Using a Powershell CMDLET

The basic syntax to add a static route using a Powershell CMDLET in Windows is shown below:

route /f /p   mask   metric  if

A brief explanation of each option is shown below:

  • /f – Used to clear the routing table.
  • /p – Used to add the routing table permanently.
  • command – Used to specify the command you want to use, like add, change, and delete.
  • destination – Used to specify the network destination.
  • mask netmask – Used to determine the subnet mask of the network destination.
  • gateway – Used to specify the gateway IP address.
  • metric – Used to identify an integer cost metric.
  • if interface – Used to specify the name of the network interface.

For example, run the following command in a Powershell CMDLET to add a static route to the destination 192.168.0.0 with the subnet mask of 255.255.0.0 and the following hop address of 192.168.0.1:

route /p add 192.168.0.0 mask 255.255.0.0 192.168.0.1 if 0x2

Now, run the following command to view the routing table.

route print

add a static route

How to Remove Specific Static Route

To remove all static routes, run the following command:

route -f

To remove a specific static routes, run the following command:

route /p delete 192.168.0.0 mask 255.255.0.0 192.168.0.1 if 0x2

delete a static route

Conclusion

The above article explained how to add and remove a static route using the PowerShell CMDLET. We also explained how to print or view your routing table. I hope you now have enough knowledge of static routing. You can now easily manage your traffic using the routing functionality.

Add a Static Route Using a Powershell CMDLET FAQs

How do I add a static route using PowerShell?

Use the New-NetRoute cmdlet to add a static route in PowerShell. Example: New-NetRoute -DestinationPrefix "10.0.0.0/24" -NextHopAddress "192.168.1.1"

How do I view static routes using PowerShell?

Use the Get-NetRoute cmdlet to view static routes in PowerShell. Example: Get-NetRoute -AddressFamily IPv4

How do I remove a static route using PowerShell?

Use the Remove-NetRoute cmdlet to remove a static route in PowerShell. Example: Remove-NetRoute -DestinationPrefix "10.0.0.0/24"

How do I modify a static route using PowerShell?

Use the Set-NetRoute cmdlet to modify a static route in PowerShell. Example: Set-NetRoute -DestinationPrefix "10.0.0.0/24" -NextHopAddress "192.168.2.1"

footer banner