bschelst/ June 8, 2019/ PowerShell/ 0 comments

It’s possible that you want to change routes on a system which point to a certain gateway, and replace them with a new gateway.

The only thing this silly powershell is doing, is getting the list of routes, filter out the ‘old gateway’, replace that gateway, and put the deletes/add in a batch script which then can be executed when you want.

This is only a silly script, nothing optimal

Write-Output ""> C:\Users\Administrator\Desktop\change_routes.bat
$Lines=Get-NetRoute |  Select-Object -Property DestinationPrefix,Nexthop | findstr "10.1.1.253"
foreach ($Line in $Lines) { 
$Line = $line.Insert(0,'route /p delete ') 
Write-Output $Line >> C:\Users\Administrator\Desktop\change_routes.bat
}

$Lines=Get-NetRoute |  Select-Object -Property DestinationPrefix,Nexthop | findstr "10.1.1.253" | %{$_.replace("10.1.1.253","10.1.1.210")} 
foreach ($Line in $Lines) { 
$Line = $line.Insert(0,'route /p add ') 
Write-Output $Line >> C:\Users\Administrator\Desktop\change_routes.bat
}

​
Share this Post

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*

eighteen − 15 =