Vasyl Stepulo
Vasyl Stepulo

Reputation: 1603

Powershell determine active ethernet adapter and his MAC

In Linux I can determine active Eth adapter, using "ip route" command:

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

Is there any way to determine which adapter name (and his MAC) used for internet connectivity in PS? Perhaps get-netadapter or something similar.

PS Right now, I'm using this code to extract MAC address from known adapter:

$CurrMac = get-netadapter | Where {$_.name -Match "Ethernet 2"}
$CurrMacaddr = $CurrMac.MacAddress.Replace("-", "")

And need to determine internet adapter automatically.

UPD1 Lets assume, that I have a host with 2 or more Eth adapters. One used for internet, rest - for internal network or inactive.

Upvotes: 0

Views: 1902

Answers (1)

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

Something like ?

$adapters = Get-NetAdapter

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

But still you can get more MAC addresses ..

Or using Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Get-NetAdapter Then

$activeMAC= Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Get-NetAdapter
Write-Host $activeMAC.MacAddress

That should return only 1 MAC

Upvotes: 0

Related Questions