Change Default Gateway with Powershell
One of the best ways to automate or check network settings with modernย Windows OS’es is to use Powershell. Recently, I had a really unique use case where the need arose to check the default gateway and flip the gateway from one value to another. Using powershell commandlets and a little bit of logic, we can easily do this. Let’s look at the topic of how to Change default gateway with Powershell.
There are a couple of commandlets that really make the configuration of the gateway possible. The get-netipaddress, get-netroute,ย remove-netroute, and new-netroute commandlets are powerful tools in manipulating the gateway value in your network configuration. Let’s see how we can do this.
Change Default Gateway with Powershell
The commandlet that we can use to query information about interfaceย aliases, ip address, address family, and other informationย about our network connection is theย get-netipaddress commandlet.
get-netipaddress
A sample of the output will look something like this:
To see information specific to your routes, we can use theย get-netrouteย commandlet. ย This will showย destination prefix, next hop, and route metrics among other things.
Changing the Default gateway
There are a couple of other action commandlets that we can use to actually configure our gateway. ย To set the gateway to a value, we must first remove what is already configured for the gateway address. ย This is where we use theย remove-netroute and then theย new-netroute commands.
We need to use theย get-netipaddress commandlet to find theย InterfaceIndexย value of the network adapter we are working with which is a required parameter to feed intoย both the “remove” and “new” netroute commands.
Using the get-netipaddress and selecting the right property, we can pass what we need into a variable to reuse as well as define our old route and new routes. ย The source can be downloaded as well from my Git link.
#variables $ipaddress='1.2.3.4' $index = get-netipaddress | where-object {$_.IPAddress -eq $ipaddress} | select -ExpandProperty InterfaceIndex $Log = 'c:windowsoptionsgatewaygatewaychange.log' $gateway = get-netroute -DestinationPrefix '0.0.0.0/0' | select -ExpandProperty NextHop $oldroute = '1.1.1.1' $newroute = '2.2.2.2' $destination = '0.0.0.0/0' #Start Changing the Gateway if needed Function Swap-Gateway() { remove-netroute -interfaceindex $index -NextHop $oldroute -confirm:$false new-netroute -interfaceindex $index -NextHop $newroute -destinationprefix $destination -confirm:$false sleep 3 } if ($gateway -eq $oldroute) { Write-Warning -Message "Gateway is set to $gateway and will be changed to $newroute" Swap-Gateway | Out-file $Log -Append } elseif ($gateway -eq $newroute) { Write-Warning -Message "Gateway is already set to $newroute and needs no change" }
The code above is intuitiveย for the most part, however, just a quick run through. ย Our variables are assigned including finding the InterfaceIndex value which is used for the remove and new netroute commands. ย This is pulled using the get-netipaddress command and filtering using the IPAddress which needs to be specified in the $ipaddress variable. ย Also, specify the $oldroute and the $newroute variables.
The function call in the “if” statement toย Swap-Gateway removes the old netroute and then specifies the new netroute. ย If the gateway is already set to the desired new route, you will see a message that it is already set to the new route and no changes are made.
Thoughts
Automation and configuration using powershell is an easy way to get a lot of work accomplished. ย There are a lot of powershell scripts that deal with Windows networking and make mass configuration changes possible very quickly and efficiently. ย The above is a simple example of how we can change default gateway with Powershellย on a Windows machine and there are a lot of other use cases we can utilize powershell to make these types of configuration changes.