How to create new routes based on existing routes with PowerShell
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 script does is get the list of routes, filter out the 'old gateway', replace that gateway, and put the deletes/adds in a batch script which can then 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
}