Hello,

I found the following script and it fits my needs almost perfectly except when it cannot find a hostname it does not notate it, I am a complete and utter powershell noob so if anyone can help modify this so it notates on the output file when it cannot find a hostname that would be amazing!

thank you,

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

Hi Jonze,

Could you do me a favor and edit your post and use the code button for your PowerShell code? It makes reading it a lot easier. Thanks!

1 Spice up

Couldn’t you just export the contents of the DNS server to achieve this? You don’t need a script to look it up as it’s right there in your DNS server.

2 Spice ups
$addresses = Get-Content C:\IP_Address.txt

$output = ForEach($address in $addresses){
    Try{
        $hostname = ([system.net.dns]::GetHostByAddress($_)).hostname
    }
    Catch {
        $hostname = 'Unresolved'
    }
    New-Object -TypeName PSObject -Property @{
        'IP' = $address
        'Hostname' = $hostname
    }
}

$output | Export-Csv -NoTypeInformation -Path "c:\out.csv"

+1 to Gary’s suggestion.

Here is how to go about what you want to do in Powershell.

3 Spice ups

Done! sorry about that

No worries :slight_smile: