Get Hostnames For Range of IP Addresses

Welcome Forums General PowerShell Q&A Get Hostnames For Range of IP Addresses

Viewing 2 reply threads
  • Author
    Posts
    • #218049
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Want to get hostnames for a corresponding range of IP addresses in an txt or csv file, had figured out the below script but the end lists the hostnames but it becomes quite difficult to understand the hostname has been assigned for which IP address, especially for a large IP range.

      Get-Content C:\IP-Address.txt | ForEach-Object {([system.net.dns]::GetHostByAddress($_)).hostname >> c:\hostname.txt}

      Can you guys please let me know, if there’s any script in powershell which i can try it out to get the desired result.

    • #218058
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Premnath, welcome to Powershell.org.

      Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!

      When you post code or error messages or sample data or console output format all this as code using the code tags “PRE“. Thanks in advance.

      If I’ve got you right you’re looking for a solution to output the hostname your code found together with the ipp address you input, right? 😉

      You can use a csv file to combine both information:

      Get-Content C:\IP-Address.txt | 
          ForEach-Object {
              [PSCustomObject]@{
                  ip = $_
                  hostname = ([system.net.dns]::GetHostByAddress($_)).hostname
              }
          } |
              Export-Csv -NoTypeInformation -Path c:\hostnameList.csv
      • #218133
        Participant
        Topics: 1
        Replies: 1
        Points: 14
        Rank: Member

        Thanks Olaf for the warm welcome and replying to my post as well with the desired response :). Stay safe and take care.

    • #218172
      js
      Participant
      Topics: 29
      Replies: 810
      Points: 2,432
      Helping Hand
      Rank: Community Hero

      Or using resolve-dnsname:

      Get-Content C:\IP-Address.txt | resolve-dnsname | select name,namehost | 
        export-csv c:\hostname.txt
      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
Viewing 2 reply threads
  • You must be logged in to reply to this topic.