PowerTip: Use PowerShell to Get IP Addresses


Summary: Use Windows PowerShell to retrieve IP addresses.

Hey, Scripting Guy! Question Is there a way to use Windows PowerShell to check IP addresses without use IPconfig?

Hey, Scripting Guy! Answer In Windows Server 2012 R2, use the new Get-NetIPAddress cmdlet:

Get-NetIPAddress

If you like the output in a table, use:

Get-NetIPAddress | Format-Table

Comments (9)

  1. Murray Wall says:

    Or you could old school it with about any version of powershell
    $ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}
    $ip.ipaddress[0]
    Will get you the ipv4 data

  2. greiginsydney says:

    This is fantastic!

    Is it possible to filter this such that it will only show the connected/active IP addresses?

    Thanks,

    G.

    1. I found that the property ‘AddressState’ is set to ‘Preferred’ when my machine connects to my domain network. So you could add that filter at the end to get the active connections.

      Get-NetIpaddress | Where addressstate -EQ preferred

      works for me.

      1. This was my first time using this cmdlet, so I didn’t even check the available parameters…but it turns out that AddressState is an available parameter when you call the cmdlet. So even quicker.

        Get-NetIPAddress -AddressState Preferred

        Shows you your currently connected adapters. Only want to see IPv4 info?

        Get-NetIPAddress -AddressState Preferred -AddressFamily IPv4

        Or if you know the name of the adapter in the Network Connections window, you can filter for just that one:

        Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4

        Of course use quotes if the adapter name is “Wireless Adapter” or “Ethernet 4” or “Local Area Connection”, etc.

  3. Ben says:

    I like to do this with $IP = test-connection “PCname” -timetolive 2 -count 1; $ip=$ip.ipv4address.

    I also check for DNS conflicts by doing this:
    $ip1=test-connection $computername -count 1 -timetolive 5; $ip2=$ip1.ipv4address
    $N2= ([system.net.dns]::gethostbyaddress(“$ip2”)).hostname
    $N1= ([system.net.dns]::gethostbyName(“$computername”)).hostname
    $p3=test-connection $N2 -count 1 -timetolive 5; $ip4=$ip3.ipv4address
    IF($N1 -eq $N2) {
    $Test=$True
    $Outcome=”$computername is on the Network and has no IP conflicts with $n1, the IP address is $ip2″
    } else {
    $Test=$false
    $outcome=”$computername has an IP conflict with $n2 with $n1 has an IP address of $ip2, This Ip address also points to $n2 with ip $ip4″
    }

  4. Robert says:

    Folks,
    I am completely lost at the moment. Any assistance would be much appreciated.
    Currently this is what I have:
    Import-module activedirectory
     
    get-adcomputer -Filter * -Searchbase "OU=ComputersOU,DC=COMPANY,DC=com" -Property * | where-object {$_.OperatingSystem -like "Windows XP*"} | select-object -property SAMAccount,Name,operatingsystem,lastlogondate | export-csv C:AllDTWinXPinAD.csv

    What I am trying to accomplish is to also pull the IPv4 address and last logged on user for each machine. Is this possible?
    Also, as a side note, any recommendations on powershell beginner guides? Powershell for dummies?
    Thank you!
    Robert

  5. @Robert: Try This: get-adcomputer -Filter * -Searchbase "OU=ComputersOU,DC=COMPANY,DC=com" -Property * | where-object {$_.OperatingSystem -like "Windows XP*"} | select-object -property SAMAccount,Name,operatingsystem,lastlogondate,IPV4Address | export-csv
    C:AllDTWinXPinAD.csv -notypeinformation

    REF:
    http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/19/powertip-use-powershell-to-get-a-list-of-computers-and-ip-addresses-from-active-directory.aspx

  6. Patricio says:

    This command don’t work on my Windows 7 (PowerShell 4.0) It’s specific for powershell 3.0 or i have to load some module before doing this?

    The comand get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1} works fine for me… (by the way, none of the Get-NetXXX commands works on my powershell)

  7. bmcmcm says:

    (Test-Connection ‘servername’ -count 1).IPv4Address.IPAddressToString

Skip to main content