Set-NetRoute How to modify properly some routes?

Hi all,

I’m trying to raise a script that can help me modifying some routes by PowerShell and I cannot find a proper way to do so.

For example, let’s say that I have 2 routes with a netmask and a gateway. For example:

ifIndex: 0 DestinationPrefix: 1.0.0.0/0 NextHop: 192.168.1.1 PolicyStore PersistentStore

and

ifIndex: 0 DestinationPrefix: 2.0.0.0/0 NextHop: 192.168.1.1 PolicyStore PersistentStore

Now, how can I modify the values for DestinationPrefix and NextHop on those two routes without deleting and adding them from scratch and using the Set-NetRoute command?

Thanks in advance.

maxi.valdez13,
Welcome to the forum. :wave:t4:

I’d get the route with Get-NetRoute along with the proper parameters and pipe it to Set-NetRoute with the desired settings. :man_shrugging:t4:

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Hi Olaf! Thank you for your reply.

I’m a new guy using PowerShell. For now, all I have is this:

Get-NetRoute -AddressFamily IPv4 -State Alive -PolicyStore PersistentStore -DestinationPrefix *.*.*.*/* | Select-Object -Propery AddressFamily,State,PolicyStore,DestinationPrefix

Now, it’s hard for me to find the proper way to set the Set-NetRoute command for the routes I want to modify. I need to modify the netmask and the gateway of some routes in particular. Is there a way to do so without deleting the routes already created?

Thank you!

How do you identify the routes you want to change? What’s the criteria?

I assume you get a list of routes when you use …

,right? So instead of *.*.*.*/* you use the desired prefix and that’s going to limit your result to the one you want to modify.

BTW: Instead of using -DestinationPrefix *.*.*.*/* you could simply omit this paramter. :wink:

Hi Olaf!

Well, the criteria is quite simple. In my organization I have different routes created for different networks.

My goal here is to create a script that can allow me to manage and edit those routes just replacing the netmask and the gateway so I can point them as I desire when I have to test migrations of the environment (with network change).

BTW, the * . * . * . * / * is in reality an actual IP address but I put it with * to resume the example.

Thank you!

In this case it is misleading to replace them with wildcards. You might have replaced them with some generic ones. :wink:

So how many result do you get when you run a query with the particular IP address? If it is only one you can omit the Select-Object and add your Set-NetRoute instead.

Hi Olaf!

Well, to be honest with you here is the part I will need some guidance because I’m not so sure how I can use the Set-NetRoute command properly.

I read some documentation but it’s a little bit tricky for me because I’m not used to write code.

As I can get, the parameters I need to change for example for the route “1.0.0.0” is the DestinationPrefix for changing the netmask and the NextHop for the gateway.

Am I right? If that’s so, how can I “set” the new netmask and gateway?

Let’s say I want to change the route 1.0.0.0/0 with gateway 192.168.1.1 to 1.0.0.0/24 with gateway 192.168.1.2.

Thank you!

Get-NetRoute -DestinationPrefix 1.0.0.0/0 -NextHop 192.168.1.1 |
    Set-NetRoute -DestinationPrefix 1.0.0.0/24 -NextHop 192.168.1.2

I urgently recommend for you to do a step back and start with learning the very basics of PowerShell first. This will save you from a lot of wasted time and frustrations.

Hi Olaf!

Thanks for this input.

I tried different ways but I’m always getting the same error that tells me the object is not found when the compilation tries to run the Set-NetRoute. Here I send you an example:

PS C:\windows\system32> Get-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168.2.0/24 -NextHop 192.168.2.1

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
0       192.168.2.0/24                                 192.168.2.1                                        1          Persiste...


PS C:\windows\system32> Get-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168.2.0/24 -NextHop 192.168.2.1 |
>> Set-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168.2.0/22 -NextHop 192.168.2.2
Set-NetRoute : Consulta CIM para instancias de la clase ROOT/StandardCimv2/MSFT_NetRoute en el servidor CIM : SELECT * FROM MSFT_NetRoute  WHERE ((DestinationPrefix LIKE
'192.168.2.0/22')) AND ((InterfaceIndex = 0)) AND ((InterfaceAlias LIKE 'iftype0[_]0')) AND ((NextHop LIKE '192.168.2.2')) no encontró objetos MSFT_NetRoute que coincidan. Compruebe los
parámetros de consulta e inténtelo de nuevo.
En línea: 2 Carácter: 1
+ Set-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MSFT_NetRoute:String) [Set-NetRoute], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound,Set-NetRoute

I don’t get why I cannot change the record when I know that it can find it properly.

Thank you!

Hmmm … I am by no means a network guy and I don’t have any experience with such a task but it looks like you cannot change a route the way you want. So you are doomed to either delete the existing one and create a new one with the desired settings or simply create a new one next to the existing one. Sorry. :man_shrugging:t4:

Hi Olaf!

Yeah, apparently this is something you “cannot” do for some reason.

Same situation happens on CMD using the route command.

Thanks anyway for your help! It was very useful for my knowlegde!

Cheers!

But it actually only needs a minor change in the code to reflect the other approach of deleting the existing one and replace it with a new one. :man_shrugging:t4:

Get-NetRoute → pipe → Remove-NetRoute
New-NetRoute

Hi Olaf,

Yeah, that can help, but I was wondering if I could avoid the deletion and the creation of the routes for the simplicity of the process.

Thanks!

As I can understand your desire to keep it simple and clean it is only one line of code more than the other approach … :man_shrugging:t4:

Hi Olaf,

You’re right about that but, on this particular case, it’s a little bit tricky because if you have 10 routes to change pointing to different networks, then you have to collect all the info first and then perform the changes for each record. It would be more practical at that point to do it manually by CMD.

As you said before, I’m doomed to that haha.

Thanks for the help!

If you have the needed information in a structured format like a CSV file you could use loop!? :man_shrugging:t4:

Hi Olaf,

That actually is a nice idea!

Thank you!