Powershell: How to figure out adapterIndex for interface to public?
@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
https://docs.microsoft.com/en-us/windows-server/networking/technologies/network-subsystem/net-sub-interface-metricWhen 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
https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/automatic-metric-for-ipv4-routes
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
- Get-NetRoute
- Set-DnsClientServerAddress
-
ForEach-Object
Standard Aliases for Foreach-Object: the '
%
' symbol, ForEach - If()