Powershell: Create new routes based on existing routes

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 your love