Finding Active IP Addresses and Resolved Hostnames with PowerShell

Posted on December 17, 2008

Ever wanted to know what IP addresses are being used on a particular subnet and what each IP address resolves to which hostname, if any?  This script accomplishes just that.

# Continue to run script when an error occurs
$ErrorActionPreference = "SilentlyContinue"
# Process through the loop of IP addresses 1-254
1..254 | foreach -Process `
    # We're combining the contents of different objects, so create our own
    $obj = New-Object PSObject;
    # Perform the Ping using WMI
    $ping = get-WmiObject -Class Win32_PingStatus -Filter ("Address='10.10.2." + $_ + "'");
    # Put the IP Address into our object
    $obj | Add-Member NoteProperty IPAddress($ping.Address);
    # Take the Ping Status Code that is returned (0 = pingable)
    $obj | Add-Member NoteProperty StatusCode($ping.StatusCode);
    # If we can ping the address, let's try to resolve the hostname
    if($ping.StatusCode -eq 0)
          # Record that the ping was successful
           $obj | Add-Member NoteProperty Status("Online");
        # Try to resolve the IP address to a hostname in DNS
        $dns = [System.Net.Dns]::GetHostByAddress($ping.Address);
           if($dns -ne $null)
            # Add the resolved hostname to our collection
             $obj | Add-Member NoteProperty ResolvedHostName($dns.HostName);
            # Couldn't resolve the IP address to a hostname
               $obj | Add-Member NoteProperty ResolvedHostName("");
        # Can't ping IP address, so mark host as offline
           $obj | Add-Member NoteProperty ResolvedHostName("");
           $obj | Add-Member NoteProperty Status("Offline");
    # Write the collection out
    Write-Output $obj;
    # Cleanup DNS object
    $dns = $null;

You might find these interesting...

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Kelli Garner

I enjoy this site, it is worth me coming back


It is good easy to understand and is working fine .

Ewan Monro

Good stuff Justin! Very well done!