I use a powershell script to manually set my DNS.

This works great, but as I move around between wifi and two different docking stations, the "ifIndex" from get-NetAdapter changes.

Just filtering for "Up" does not fly, because there are multiple Up interfaces (see below)

I am thinking I need to detect the interface that is both Up and is the route to public (e.g. on the route to google.com or something public).

How to do this?

PS C:\Users\joe> get-netadapter Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet TAP-ProtonVPN Windows Adapter V9 32 Disconnected 00-FF-F9-EE-DD-E4 1 Gbps VMware Network Adapte...1 VMware Virtual Ethernet Adapter for ... 30 Up 00-50-56-EE-DD-01 100 Mbps Wi-Fi Dell Wireless 1820A 802.11ac 25 Up C8-FF-28-EE-DD-4D 702 Mbps VMware Network Adapte...8 VMware Virtual Ethernet Adapter for ... 24 Up 00-50-56-EE-DD-08 100 Mbps Bluetooth Network Conn... Bluetooth Device (Personal Area Netw... 15 Disconnected C8-FF-28-EE-DD-4E 3 Mbps vEthernet (Default Swi... Hyper-V Virtual Ethernet Adapter 22 Up 00-15-5D-EE-DD-F0 10 Gbps vEthernet (WSL) Hyper-V Virtual Ethernet Adapter #2 38 Up 00-15-5D-EE-DD-00 10 Gbps 

2 Answers

@Drink More Pimp Juice IT already mentioned a simple solution, which works if there is only one network adapter with a default gateway enabled and active.

If you have several network adapters enabled and active at the same time, which happens when you use a Laptop which doesn't disable or disconnect the WiFi adapter when connected via Ethernet, you will have two routes with a route metric of 0, but with different InterfaceMetric's.

When the routes are manually configured it's also possible that the route metric of the default route isn't 0 at all.

Solution

If you want to get the global default route of the Operating System in such cases, you have to take the total of RouteMetric and InterfaceMetric and use the default routes for IPv4 and IPv6 as destination filter.

The following command gets the default route of your OS using the IPv4 and IPv6 default routes.

Get-NetRoute -DestinationPrefix '0.0.0.0/0', '::/0' | Sort-Object -Property { $_.InterfaceMetric + $_.RouteMetric } | Select-Object -First 1 

You can remove the IPv6 destination prefix '::/0' if you only want to determine the IPv4 route.

With the returning Interface Index ifIndex you can change the respective network adapter. It's also possible to pipe the output above directly to Get-NetAdapter or any other cmdlet which accepts the InterfaceIndex as named property.

PowerShell 6+

Beginning with PowerShell 6, we can simplify the command, since there is the new parameter Top available for Sort-Object.

Get-NetRoute -DestinationPrefix '0.0.0.0/0', '::/0' | Sort-Object -Property { $_.InterfaceMetric + $_.RouteMetric } -Top 1 

Informative resources

  • Configure the Order of Network Interfaces

    When network traffic routes are chosen and you have configured the InterfaceMetric parameter of the Set-NetIPInterface command, the overall metric that is used to determine the interface preference is the sum of the route metric and the interface metric. Typically, the interface metric gives preference to a particular interface, such as using wired if both wired and wireless are available.

  • An explanation of the Automatic Metric feature for IPv4 routes

2

You can use get-netroute to get the adapter index number with the 0 or false value which is the lowest metric and thus the adapter which should be the default for traffic to use.

PowerShell

Get-NetRoute | % { Process { If (!$_.RouteMetric) { $_.ifIndex } } }; Set-DNSClientServerAddress –interfaceIndex $intix –ServerAddresses ("127.0.0.1","1.1.1.2"); 

Output Sample

Command: Get-NetRoute | Select InterfaceAlias, InterfaceIndex, RouteMetric | FL

InterfaceAlias : Loopback Pseudo-Interface 1 InterfaceIndex : 1 RouteMetric : 256 InterfaceAlias : Wi-Fi 3 InterfaceIndex : 7 RouteMetric : 0 InterfaceAlias : Local Area Connection* 12 InterfaceIndex : 14 RouteMetric : 256 InterfaceAlias : Local Area Connection* 11 InterfaceIndex : 27 RouteMetric : 256 

Supporting Resources

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy