Get Computer by IP Address

Get-QADComputer can accept lots of forms of computer identification: name, DNS name, DN, and so on, but IP address is currently not there. Fear not! PowerShell and .NET can do wonders.

With a little bit of help from Jeffrey, I scripted a simple function which turns IP addresses into computer names and supports every possible scenario I could think of:

# No parameters
PS C:\> Get-ComputerNameByIP
Please supply the IP Address: 10.20.30.40
mycomp.domain.local

# With parameter
PS C:\> Get-ComputerNameByIP 10.20.30.40
mycomp.domain.local

# With pipeline
PS C:\> '10.20.30.40', '10.20.30.41' | Get-ComputerNameByIP
mycomp.domain.local
myserv.domain.local

# Or reading from a text file (one IP address per line)
PS C:\> get-content C:\temp\ip_addresses.txt | Get-ComputerNameByIP
mycomp.domain.local
myserv.domain.local

# Or piping that further to get AD computer objects
PS C:\> '10.20.30.40', '10.20.30.41' | Get-ComputerNameByIP | 
               ForEach-Object { Get-QADComputer -DnsName $_ }

Name    Type      DN
----    ----      --
MYCOMP  computer  CN=MYCOMP,OU=Redmond,OU=US,OU=Desktops...
MYSERV  computer  CN=MYSERV,OU=Redmond,OU=US,OU=Servers,...

And here’s the actual function which you can copy/paste into your PowerShell window, add to your PowerShell profile, or “dot-source” from an external file.

function Get-ComputerNameByIP {
    param(
        $IPAddress = $null
    )
    BEGIN {
    }
    PROCESS {
        if ($IPAddress -and $_) {
            throw 'Please use either pipeline or input parameter'
            break
        } elseif ($IPAddress) {
            ([System.Net.Dns]::GetHostbyAddress($IPAddress)).HostName
        } elseif ($_) {
            [System.Net.Dns]::GetHostbyAddress($_).HostName
        } else {
            $IPAddress = Read-Host "Please supply the IP Address"
            [System.Net.Dns]::GetHostbyAddress($IPAddress).HostName
        }
    }
    END {
    }
}

Have fun!

Tags: , , , ,

15 Responses to “Get Computer by IP Address”


  1. 1 Mike Cates March 20, 2008 at 4:36 pm

    Hello,
    Great script, but i would like to be able to run this to gather all computers on my subnet and pipe it to a file that list:

    Name Type DN IPADDRESS
    MYSERV computer CN=MYSERV,OU=Redmond,OU=US,OU=Servers, 10.10.10.1

    Could you help me out on this one?

  2. 2 Mike Cates March 20, 2008 at 4:44 pm

    I did not make this clear, but i would like to give the script a range. IE. 10.10.20.1 to 10.10.20.255 and for it to list on that subnet to a file..

  3. 3 dmitrysotnikov March 20, 2008 at 5:28 pm

    Mike, here’s how I would supply the range:

    10..15 | ForEach-Object {“10.20.30.$_”} | Get-ComputerNameByIP

    So I am basically supplying the actual range in the beginning and the constant part in the second (foreach) section putting the variable part inside the string.

    However, because the range might contain invalid IP addresses I would add some error handling to this. Here’s the updated function with error handling (I have also removed .HostName so full objects with IP addresses are returned – not just the names):

    function Get-ComputerNameByIP {
    param(
    $IPAddress = $null
    )
    BEGIN {
    }
    PROCESS {
    if ($IPAddress -and $_) {
    throw ‘Please use either pipeline or input parameter’
    break
    } elseif ($IPAddress) {
    ([System.Net.Dns]::GetHostbyAddress($IPAddress))
    } elseif ($_) {
    trap [Exception] {
    write-warning $_.Exception.Message
    continue;
    }
    [System.Net.Dns]::GetHostbyAddress($_)
    } else {
    $IPAddress = Read-Host “Please supply the IP Address”
    [System.Net.Dns]::GetHostbyAddress($IPAddress)
    }
    }
    END {
    }
    }

  4. 4 IP April 28, 2008 at 10:29 am

    Thanks for the article. I don’t know why this script not running on my computer for some IP Addresses.

  5. 5 dmitrysotnikov April 28, 2008 at 10:43 am

    Probably DNS lookups failing. What do you get if you do this for some of the failing addresses?

    [System.Net.Dns]::GetHostbyAddress( ‘10.20.30.40’ )

  6. 6 GW July 17, 2008 at 9:53 pm

    Hi Dmitry, is there way to add functionality to do lookups against a particular DNS server? I didn’t seem to see that available in the .Net DnsClass…

  7. 7 dmitrysotnikov July 18, 2008 at 7:37 am

    GW, frankly, I don’t know. As you can see I am just using the .NET System.Net.DNS class, and I don’t know whether it is possible to make it work against a particular server. The MSDN documentation does not seem to provide an answer: http://msdn.microsoft.com/en-us/library/system.net.dns.gethostbyaddress.aspx

  8. 8 dean June 1, 2009 at 6:29 pm

    Dmitry,

    Is there a way to have a script read a txt file that contains a list of IP address and then returns the IP address and the computer name and formated like below:

    IP ComputerName
    10.1.1.1 MikesComputer
    10.1.1.2 FranksDevBox

    thank you for your help

  9. 9 Dmitry Sotnikov June 3, 2009 at 12:37 pm

    Dean,

    Sure, suppose you have a text file with IP addresses, one at each line:

    get-content IPAddresses.txt | ForEach-Object {
    [System.Net.Dns]::GetHostbyAddress($_) |
    Add-Member -Name IP -Value $_ -MemberType NoteProperty -PassThru
    } | Select IP, HostName | Export-CSV IPandName.csv

    To get faster response in the future try asking such questions in our forums: http://www.powergui.org/forumindex.jspa?categoryID=55

    Dmitry

  10. 10 dean June 3, 2009 at 4:19 pm

    where would error handling be placed if the IP is not attached to anything (not on the wire)

  11. 11 John Middleton June 3, 2009 at 7:13 pm

    So.. how can I work this process backwards.. I have server in a DMZ and need their IP addresses .. How can I supply a server name and use .NET to return the IP address..?

    Been trying to use System.Net.NetworkInformation.Ping but it reurns the IP as a combined number of some sort…??

    Thx

  12. 12 John Middleton June 3, 2009 at 7:28 pm

    So.. how can I work this process backwards.. I have server in a DMZ and need their IP addresses .. How can I supply a server name and use .NET to return the IP address..?

    Been trying to use System.Net.NetworkInformation.Ping but it reurns the IP as a combined number of some sort…??

    Thx

  13. 13 Petr Kutalek December 14, 2010 at 4:20 pm

    Hello John,
    backward function could be:

    function Get-ComputerIPByName {
    param(
    $ComputerNames = $null
    )
    BEGIN {
    }
    PROCESS {
    if ($ComputerNames -and $_) {
    throw ‘Please use either pipeline or input parameter’
    break
    } elseif ($ComputerNames) {
    ([System.Net.Dns]::GetHostbyName($ComputerNames))
    } elseif ($_) {
    trap [Exception] {
    write-warning $_.Exception.Message
    continue;
    }
    [System.Net.Dns]::GetHostbyName($_)
    } else {
    $ComputerNames = Read-Host “Please supply the computer names”
    [System.Net.Dns]::GetHostbyAddress($ComputerNames)
    }
    }
    END {
    }
    }

    And thank you very much Dmitry 🙂

  14. 14 Ip Address May 31, 2013 at 7:20 am

    Hi! I’ve been reading your web site for a long time now and finally got the courage to go ahead and give you a shout out from Lubbock Texas! Just wanted to say keep up the great work!

  15. 15 Manfred November 11, 2021 at 1:17 am

    Hi Dimi,

    finally >13 years after you have written your funbtion, I’ve finally found it.
    Only sad thing is that it is not working with the latest Exchange Server OR in my enviroment.

    Any new version of it?


Leave a comment




Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

March 2008
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31