Prioritizing Windows Network Connections

Prioritizing Windows Network Connections

Windows automatically prioritizes network connections that have multiple routes available based on the the interface speed, for example if you are connected to the same network through WiFi and hard-wire. It may, at times, be necessary to prioritize network traffic out a different interface, for example if you are testing a specific network connection. This can be done by adjusting the IPv4 Interface Metric on the adapters.

Viewing Interface Metrics

The following command will output the network interfaces on the local computer sorted by the interface metric. Windows will prioritize network traffic over lower metrics.

> Get-NetIPInterface | Sort-Object InterfaceMetric

ifIndex InterfaceAlias                  AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp     ConnectionState PolicyStore
------- --------------                  ------------- ------------ --------------- ----     --------------- -----------
58      vEthernet (Ethernet 3)          IPv4                  1500              15 Disabled Connected       ActiveStore
46      vEthernet (vEthernet (LAN))     IPv4                  1500              15 Disabled Connected       ActiveStore
40      vEthernet (Default Switch)      IPv4                  1500              15 Disabled Connected       ActiveStore
21      vEthernet (LAN)                 IPv4                  1500              25 Enabled  Connected       ActiveStore
1       Loopback Pseudo-Interface 1     IPv6            4294967295              75 Disabled Connected       ActiveStore
1       Loopback Pseudo-Interface 1     IPv4            4294967295              75 Disabled Connected       ActiveStore
52      Ethernet 3                      IPv4                  1500              99 Enabled  Connected       ActiveStore

Get-NetIPInterface | Sort-Object InterfaceMetric

Viewing Routes for a Specific Address

If you’d like to look at a route for a specific destination, you can search based on the network prefix:

> Get-NetRoute -DestinationPrefix 192.168.2.0/24

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
52      192.168.2.0/24                                 0.0.0.0                                          256 99       ActiveStore

If you want to query for addresses on the internet, you’ll most likely need to look at the default route:

> Get-NetRoute -DestinationPrefix 0.0.0.0/0

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
52      0.0.0.0/0                                      192.168.2.1                                        0 25       ActiveStore
21      0.0.0.0/0                                      192.168.86.254                                     0 25       ActiveStore

As you can see 0.0.0.0/0 has 2 possible routes with the same metric, so you can’t guarantee which one traffic will take.

Changing Interface Metrics

To change the metrics for a specific interface, you can navigate to the Advanced IPv4 settings in the network control panel, or just change it with PowerShell

> Set-NetIPInterface -ifIndex 52 -InterfaceMetric 99

The command won’t return anything, but running the Get-NetIPInterface command again will show that the interface has a metric of 99 which is higher than anything Windows will automatically assign to an interface.

A PowerShell Function

As a parting note, I decided to write a simple function in PowerShell to help gather all the pertinent information that I often times need to get quickly. The below function returns the following information about each network interface:

  • Interface Alias - What you can expect to see in Control Panel as the name of each interface
  • Interface Index - The index that you'd use for other PowerShell networking commands
  • Interface Description - The description that you'll see in Control Panel
  • Address - The CIDR representation of the IP address and subnet mask
  • Metric - I don't think I have to explain this again
  • Speed - The physical link speed
function Get-NetInterfaceInformation {
  [CmdletBinding()]
  param (
    [ValidateSet("IPv4","IPv6")]
    [String]$AddressFamily
  )
  
  Get-NetIPInterface @PSBoundParameters | Sort-Object InterfaceMetric | ForEach-Object {
    if($_.ifIndex -eq 1) { return; }
    $adapter = $_ | Get-NetAdapter
    $address = $_ | Get-NetIPAddress
    [PSCustomObject]@{
      InterfaceAlias=$_.InterfaceAlias;
      InterfaceIndex=$_.ifIndex;
      InterfaceDescription=$adapter.InterfaceDescription;
      Address="$($address.IPAddress)/$($address.PrefixLength)"
      Metric=$_.InterfaceMetric;
      Speed=$adapter.LinkSpeed;
    }
  }
}