powershellnic

get IP address of all active network interfaces


I'm trying to get the IPv4 address of interfaces that are connected in PowerShell, so I tried this :

 Get-NetIPAddress -AddressFamily IPv4  | select InterfaceAlias , IPAddress | Sort-Object InterfaceAlias

But I noticed it output more interfaces than expected so I tried this :

PS C:\> Get-NetIPInterface -ConnectionState Connected | % { Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias $_.InterfaceAlias } | select InterfaceAlias , IPAddress

InterfaceAlias              IPAddress
--------------              ---------
WiFi                        192.168.1.145
Loopback Pseudo-Interface 1 127.0.0.1
WiFi                        192.168.1.145
Loopback Pseudo-Interface 1 127.0.0.1


PS C:\>

But then I see twice the same lines.

I expected this result :

InterfaceAlias              IPAddress
--------------              ---------
WiFi                        192.168.1.145
Loopback Pseudo-Interface 1 127.0.0.1


PS C:\>

How can this be done ?


Solution

  • Olaf has provided the solution in a comment; let me elaborate:

    Therefore:

    Get-NetIPInterface -ConnectionState Connected -AddressFamily IPv4 | 
      Get-NetIPAddress |
      Select-Object InterfaceAlias, IPAddress |
      Sort-Object InterfaceAlias