Skip to content Skip to sidebar Skip to footer

what powershell cmdlet can be used to add a static route?​

We are reader supported and may receive a commission when y'all brand purchases using the links on our site.

How to add a static route using a PowerShell cmdlet

We show you how to add static routes and its associated parameters to a routing table in Windows using PowerShell cmdlets

Add a static route using a PowerShell cmdlet

1 of the seldom-known facts about a Windows automobile is that information technology tin be configured to perform the functions of a primary router

The Windows operating system has a congenital-in routing table that enables it to brand routing decisions. Suppose you take a small office with a meager IT budget, or you need a primary router for a temporary network that does not crave high security. In that case, you lot tin save money by adding routing functions to an existing Windows PC or server rather than purchasing a hardware router. Then, yous simply demand to take care of basic security and ensure that the server you're using isn't running short on arrangement resources, such as deejay space, memory, or CPU time.

The term routing is used to describe taking a packet from 1 device and sending it through the network to another device on a unlike network. First, the logical network address of the destination host is used to get packets through a routed network. Then the host's hardware address (MAC address) is used to deliver the packet to the right destination host.

The routing device learns about remote networks from bordering routing devices or the network ambassador. The routing device then builds a routing table (a map of the internetwork) that describes finding the remote networks. If a network is straight continued, the router or routing device already knows how to get to information technology. If a network isn't directly related to the router, the router must utilize 1 of two ways to learn how to get to the remote network:

  • Dynamic routing: Routing information is dynamically or automatically configured or entered
  • Static routing: Routing information is manually configured or entered

This article will testify you how to add static routes and implement static routing on a Windows car using PowerShell cmdlets.

Why add Static Routes?

Most routers—including the one built into your Windows PC—use some form of dynamic routing. However, there are occasions where static routing may exist used; in that example, the network ambassador is responsible for manually updating all changes and adding static routes to the routing device. Here are a few occasions where you lot may demand to implement static routes:

  • Due to security considerations, specific routes can't be added to the default gateway device, and you require another routing device to perform certain functions. In that instance, a local Windows computer can be configured to perform the required routing functions.
  • You take ready multiple subnets or VLANs on your network and need to direct traffic to a particular subnet. Static routes can be particularly useful in testing these types of environments.
  • You have two internet routers (principal and secondary) on the network, and you want to utilise the secondary router for sending social media and audio/video streaming services traffic (Facebook, Instagram, Twitter, YouTube, Netflix, Spotify, etc.), and the primary router for sending all other traffic.
  • You're currently using a Windows PC as a router for your network, and you want to have meliorate control of the routing table and the flow of traffic.in and out of your network.

What is a Routing Table?

Whenever a network device needs to send data to another device on a network, information technology must commencement know where to send information technology. If the network device cannot directly connect to the destination device, it has to send it via other devices forth a route to the destination device. Each device needs to keep runway of which way to deliver various packets of data, and for this, it uses what we call a routing table.

A routing tabular array is analogous to a distribution map in bundle delivery. Information technology is a database that keeps track of paths, like a map, and uses these to determine which mode to forward traffic or when the traffic leaves a routing device—whether it'due south a physical router or a PC. Therefore, it's essential to look at your existing routing tabular array before making changes to information technology. Follow the steps below to view the routing table:

  1. Type 'powershell' in the Windows search bar to locate the PowerShell app.
  2. Right-click on the app and click "Run as ambassador".
  3. One time the PowerShell command prompt opens, type the following command to view the routing table: route impress.
output of the route print command
Figure one.0 | Screenshot showing the output of the route impress command

Equally you lot can see from the screenshot above, the IPv4 routing table consists of the following fields:

  • Network Destination: This field lists all of the network segments that the router is attached to.
  • Netmask: The Netmask column provides the subnet mask non of the network interface attached to the segment but of the segment itself. This allows the router to make up one's mind the address class for the destination network.
  • Gateway: Once the router has adamant which destination network it needs to ship the packet to, it looks at the gateway list, which tells the router which IP accost the packet should be forwarded through to attain the destination network.
  • Interface: The Interface column tells the router which NIC is continued to the appropriate destination network.
  • Metric: The routing metric of the path through which the packet is to exist sent. The route will go in the direction of the gateway with the lowest metric.

Persistent Routes: Persistent routes helps to preserve the routes from being removed from the Windows operating organisation one time it is rebooted. This contrasts to non-persistent (active) routes, which are temporary and will be erased later on the arrangement is rebooted.

Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 192.168.44.37 192.168.43.57 10
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
192.168.0.0 255.255.255.0 192.168.0.100 192.168.0.100 25
192.168.0.100 255.255.255.255 127.0.0.one 127.0.0.1 25

How practice you add a Static Route to the Windows Routing Table?

The syntax for adding static route to a routing table in a Windows-based routing device is a follows:

road [/f] [/p] [<command> [<destination>] [mask <netmask>] [<gateway>] [metric <metric>]] [if <interface>]]

The critical parameters for the add static route syntax are described as follows:

  • -f Clears the routing table of all entries that do not host routes (routes with a netmask of 255.255.255.255) and the loopback network road.
  • -p When used with the add together command, the specified route is added to the registry and is used to initialize the IP routing tabular array whenever the TCP/IP protocol is started.
  • Control Specifies the control you desire to run. The valid commands include: (add, alter, delete, impress)
  • Destination Specifies the network destination of the route.
  • mask Netmask Specifies the netmask (subnet mask) associated with the network destination
  • Gateway Specifies the forwarding or post-obit hop IP address over which the set up of addresses divers by the network destination and subnet mask are reachable
  • Metric Specifies an integer cost metric (ranging from ane to 9999) for the route, which is used when choosing amidst multiple routes in the routing table that most closely match the destination address of a packet being forwarded. The route with the lowest metric is chosen.
  • If Interface Specifies the alphabetize of the interface over which the destination is reachable.

Now, to show a practical example in PowerShell using the to a higher place syntax, follow the steps below to enter a static route to the destination ten.51.0.0 with the subnet mask of 255.255.0.0 and the post-obit hop address of 10.23.0.1:

  1. Blazon 'powershell' in the Windows search bar to locate the PowerShell app.
  2. Right-click on the app and click Run as administrator.
  3. One time the PowerShell control prompt opens, type the following control to add the static road.

route add 10.51.0.0 mask 255.255.0.0 x.23.0.1

Static route with persistent: As stated earlier, this route will only stay in the routing table until Windows is rebooted. Once that happens, the contents of the routing table will be erased. Therefore, if you want the entry to persist, you need to append -p  to the in a higher place command. For example, to add a persistent route to the destination ten.51.0.0 with the subnet mask of 255.255.0.0 and the next hop address of ten.23.0.i, blazon the post-obit command:

route /p add 10.51.0.0 mask 255.255.0.0 x.23.0.1

Static road with metric: To add a road 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 toll metric of 65, blazon the following command:

route add together 10.51.0.0 mask 255.255.0.0 x.23.0.1 metric 65

Static road with an interface: The interface list numbers are displayed just before the IPv4 route table when using the route impress command to view the routing table (please see figure 2.0 below). Index number 1 (0x1) is for loopback, and the rest are assigned to other existing network interfaces. For example, to add together a route to the destination 10.51.0.0 with the subnet mask of 255.255.0.0, the post-obit hop address of 10.23.0.1, and using the interface alphabetize 0x7, blazon the following control:

route add 10.51.0.0 mask 255.255.0.0 10.23.0.one if 0x7

Screenshot highlighting the interface list numbers
Effigy 2.0 | Screenshot highlighting the interface listing numbers
Screenshot showing the newly added static route entries
Figure iii.0 | Screenshot showing the newly added static route entries

Conclusion

This commodity explained how to add a static route and other vital parameters to a routing table using PowerShell. If you want to run into the contents of your entries,  utilise the route print command to view the routing table, and you'll see all your static route entries. Nosotros hope this simple tutorial helps yous better sympathize the concept of the static route and how to implement it in Windows operating systems. Once once again, remember to run PowerShell as ambassador when executing these commands. If y'all need more help, blazon the control route/? to see more syntax examples.

smallwoodfixecition.blogspot.com

Source: https://www.comparitech.com/net-admin/add-static-route-using-powershell-cmdlet/

Post a Comment for "what powershell cmdlet can be used to add a static route?​"