Search code examples
powershellbatch-filenetwork-programming

How can I identify the network adapter (or interface) name used for a successful ping in a window bat or powershell


Is there a way to identify the network adapter (or interface) name used for a successful ping

eg I have the following adapters: Network Adapters

I perform the command

ping google.com

which is successful, I would like to know that the adapter used is either "Wi-Fi" Sunrise_5GHz_387918

There is a similar question for c# Identifying active network interface

But I am looking for a windows batch file way (possibly powershell).


Solution

  • You can do that in PS using:

    get-wmiobject win32_networkadapter | select netconnectionid, name, InterfaceIndex, netconnectionstatus
    

    OR, You can use netsh

    netsh interface ipv4 show interfaces
    

    In the newer version of PS like in windows 8, you can even directly use:

    Get-NetAdapter | SELECT name, status, speed, fullduplex | where status -eq 'up'
    

    Note by that the netconnectionstatus value indicates the connection status. 2 indicates connected