Skip to content

Get-IPAddressInfo

Windows: Retrieves IP address configuration for network adapters

#Requires -Version 5.1
#Requires -Modules NetTCPIP

[CmdletBinding()]
Param (
    [string]$InterfaceAlias = "*",

    [ValidateSet('IPv4', 'IPv6')]
    [string]$AddressFamily,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process {
    try {
        $session = $null
        $ipParams = @{
            'ErrorAction' = 'Stop'
        }
        if (-not [string]::IsNullOrWhiteSpace($AddressFamily)) {
            $ipParams.Add('AddressFamily', $AddressFamily)
        }

        if ($ComputerName -ne $env:COMPUTERNAME) {
            $sessionParams = @{
                'ComputerName' = $ComputerName
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential) {
                $sessionParams.Add('Credential', $Credential)
            }
            $session = New-CimSession @sessionParams
            $ipParams.Add('CimSession', $session)
        }

        $addresses = Get-NetIPAddress @ipParams | Where-Object { $_.InterfaceAlias -like $InterfaceAlias }

        $results = foreach ($addr in $addresses) {
            [PSCustomObject]@{
                InterfaceAlias = $addr.InterfaceAlias
                IPAddress      = $addr.IPAddress
                AddressFamily  = $addr.AddressFamily
                PrefixLength   = $addr.PrefixLength
                Type           = $addr.Type
                ComputerName   = $ComputerName
            }
        }

        Write-Output ($results | Sort-Object InterfaceAlias, IPAddress)
    }
    catch {
        throw
    }
    finally {
        if ($null -ne $session) {
            Remove-CimSession $session
        }
    }
}

Specifies the friendly name of the network interface (e.g., "Ethernet"). Supports wildcards.

Specifies the IP address family. Valid values: IPv4, IPv6.

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.