The default gateway setting in Windows (and every other networked operating system) is a simple setting that tells your network interface card (NIC) where to send traffic when sending it outside of your domain segment. More often than not, it will be the .1 address of a network segment (e.g.: 10.0.0.1), but that is not always the case.
It is one of those settings that you set once and forget it… It almost never needs to be changed… until it does. Network reconfigurations do happen, and changing the default gateway is simple to do in the graphical user interface via the Properties window of your network interface, simply by modifying the appropriate field in the Internet Protocol Version 4 (TCP/IPv4) properties.
But what if you need to do it for several machines? Of course, PowerShell to the rescue!
First, you need to check what your NIC Interface Index is:
This will give you an output that looks like this:
As we see in this example, the server was moved from one network segment (10.128.43.x/24) to a new one (10.128.11.x/24). Because of that, we need to assign a new Gateway in the proper network segment.
The Interface Index here lists as 3. Remember that.
Before we add the new Gateway, we have to remove the old one. Otherwise the NIC will have two gateways, and that can cause issues.
Remove-NetRoute -ifindex 3 -NextHop “10.128.43.1”
Notice that we put in 3 for the ifindex (the Interface Index), and the old gateway in quotes.
Now that we have a clean slate, all we have to do is configure the new default gateway, with this:
New-NetRoute -interfaceindex 3 -NextHop “10.128.11.1” -destinationprefix “0.0.0.0/0”
Again, we change our interfaceindex to 3, and our NextHop to the proper gateway. When you run these two commands, you should get the following output:
That’s all there is to it! Of course, you may want to execute this script against a group of computers, but that’s for another time…