Windows: change the default gateway with PowerShell
Replace the default route of a Windows machine with Remove-NetRoute and New-NetRoute.
This page shows how to change the default gateway (the 0.0.0.0/0 route) of a Windows machine with PowerShell, for example to move it from a management interface to a production interface.
Principle
The default gateway is a network route to 0.0.0.0/0. To change it, remove the existing route with Remove-NetRoute, then create a new one with New-NetRoute on the wanted interface.
Example
Here, the gateway 192.0.2.10 on the MGT-NET interface is replaced by gateway 192.0.2.11 on the PRD-NET interface.
# Remove the old default gateway
Remove-NetRoute -InterfaceAlias "MGT-NET" -NextHop "192.0.2.10"
# Create the new default gateway
New-NetRoute -InterfaceAlias "PRD-NET" -NextHop "192.0.2.11" -DestinationPrefix "0.0.0.0/0"
For another environment, reuse the same removal and give a different gateway:
Remove-NetRoute -InterfaceAlias "MGT-NET" -NextHop "192.0.2.10"
New-NetRoute -InterfaceAlias "PRD-NET" -NextHop "192.0.2.12" -DestinationPrefix "0.0.0.0/0"
Notes
-InterfaceAliasis the network adapter name (seeGet-NetAdapter).- Use straight quotes (
"), not typographic quotes, otherwise PowerShell returns an error.