locked
Determine which ethernet adapter is used for the internet with Powershell RRS feed

  • Question

  • Lets assume, that I have a host with 2 ethernet cards. One connected to internal network (without internet access), and another to different network with internet access.

    In Linux I have a command, to deternine, which adapter used to connect to the internet:

    vasyl@retail-z3-1:~$ ip route get 8.8.8.8
    8.8.8.8 via 10.186.0.1 dev ens4  src 1.1.1.1

    So, I know, that ens4 has internet connectivity.

    Is there any way to determine which adapter name used for internet connectivity in PowerShell?

    PS I have a following code to get all connected adapters - how to determine adapter, used for internet connectivity?

    $adapters = Get-NetAdapter
    
    foreach ($adapter in  $adapters){
        if($adapter.Status -eq "Up" -and $adapter.Name.Contains("Ethernet")){
        Write-Host $adapter.MacAddress 
        }
    }

    Thursday, April 11, 2019 9:08 AM

Answers

  • Get-NetAdapter | Where{$_.Status -eq 'up'} | select -Expand Name

    Get-NetConnectionProfile | Where{ $_.IPv4Connectivity -eq 'Internet'}


    \_(ツ)_/

    • Marked as answer by Vegas_85 Thursday, April 11, 2019 10:16 AM
    Thursday, April 11, 2019 9:29 AM

All replies

  • Hello,

    In windows you can use "get-netroute" or "route print" to list the route list.

    With this way you can determine which path your command will follow.

    If you want to add a new route you can use the route add command :

    "route ADD destination MASK subnet-mask  gateway_ip cost"

    or new-netroute command :

    https://docs.microsoft.com/en-us/powershell/module/nettcpip/new-netroute?view=win10-ps

    Thursday, April 11, 2019 9:18 AM
  • Get-NetAdapter | Where{$_.Status -eq 'up'} | select -Expand Name

    Get-NetConnectionProfile | Where{ $_.IPv4Connectivity -eq 'Internet'}


    \_(ツ)_/

    • Marked as answer by Vegas_85 Thursday, April 11, 2019 10:16 AM
    Thursday, April 11, 2019 9:29 AM
  • Get-NetAdapter | Where{$_.Status -eq 'up'} | select -Expand Name

    Get-NetConnectionProfile | Where{ $_.IPv4Connectivity -eq 'Internet'}


    \_(ツ)_/


    Thanks! Works like a charm!
    Thursday, April 11, 2019 10:17 AM