Getting network interface device name in powershell

Solution 1:

Finally, found the solution! Since it does show up correctly in the registry and device manager, we can get it from the related Win32_PnPEntity object. So the code becomes:

$interfaces = Get-WmiObject Win32_NetworkAdapter
$interfaces | foreach {
    $friendlyname = $_ | Select-Object -ExpandProperty NetConnectionID
    $name = $_.GetRelated("Win32_PnPEntity") | Select-Object -ExpandProperty Name
    "$friendlyname is $name"

This matches perfectly with the names in device manager and network connections on all of the systems I have tried it on.

Solution 2:

I'm not sure if this is your whole code, but with your current loop, you are just writing over the $friendlyname and the $name variables. It might be that the last item was the one without the number on it. The following code gave me the results you are looking for:

$interfaces = Get-WmiObject Win32_NetworkAdapter
$Outputs = @()
foreach ($Interface in $Interfaces) { 
    $Output = New-Object -TypeName System.Object
    $Output | Add-Member -MemberType NoteProperty -Name FriendlyName -Value $Interface.NetConnectionID
    $Output | Add-Member -MemberType NoteProperty -Name Name -Value $Interface.Name
    $Outputs += $Output

You can then use $outputs | out-grid to see all that was listed on your system.