PowerShell Genie

Review default routes

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Filtering Get-NetRoute to the destination prefix 0.0.0.0/0 returns only the default routes.

The script

1PS> Get-NetRoute -DestinationPrefix 0.0 . 0.0 / 0 | Select-Object ifIndex,NextHop,RouteMetric,InterfaceAlias

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 ifIndex NextHop RouteMetric InterfaceAlias
    2 ------- ------- ----------- --------------
    3 12 10.20.30.1 25 Ethernet
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-NetRoute -DestinationPrefix 0.0 . 0.0 / 0 | Select-Object ifIndex,NextHop,RouteMetric,InterfaceAlias
    1. Stage 1Get-NetRoute-DestinationPrefix
    2. Stage 2Select-ObjectPositional values or no named parameters

      How it works

      Filtering Get-NetRoute to the destination prefix 0.0.0.0/0 returns only the default routes. Select-Object reports the interface index and alias next to NextHop and RouteMetric, and the metric is what decides the winner whenever more than one default is present.

      Adapting it

      Drop the -DestinationPrefix filter and pipe into Where-Object NextHop -ne '0.0.0.0' to review every off-link route instead of just the defaults.

      If the result is unexpected

      Traffic leaves through the wrong interface even though the intended gateway appears in the output holding the lower metric.

      Route selection compares the sum of the route metric and the interface metric, so an attractive RouteMetric can still lose because its interface metric is high. When the totals tie, the choice falls to binding order. Windows also assigns interface metrics automatically from link speed unless that behavior has been overridden.

      1. Read the interface metrics from Get-NetIPInterface and add them to each candidate's RouteMetric.
      2. Run Find-NetRoute with a destination address to see which route the stack actually chooses.
      3. Set an explicit value with Set-NetIPInterface -InterfaceMetric rather than adjusting the route itself.
      4. Disable automatic metric assignment on the interfaces involved so the values stop shifting with link state.
      5. Look for a second default route added by a VPN client, which is a common and temporary cause.

      Validate the result

      Find-NetRoute for a representative destination returns the intended interface, and a traceroute from the host leaves through the expected gateway.

      Keep the check reliable

      Set interface metrics explicitly on multi-homed servers instead of relying on automatic assignment, and write down the intended egress path.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideReview default routes

      Filtering Get-NetRoute to the destination prefix 0.0.0.0/0 returns only the default routes.