Get Hostnames For Range of IP Addresses

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.

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? :wink:

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

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

Or using resolve-dnsname:

Get-Content C:\IP-Address.txt | resolve-dnsname | select name,namehost | 
  export-csv c:\hostname.txt