Powershell

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-netrouteremove-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:

gateway01

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.

gateway02

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.

Back to top button