Skip to content

Instantly share code, notes, and snippets.

@ShortArrow
Created May 17, 2024 01:45
Show Gist options
  • Save ShortArrow/020e9408fb79d138961152d74b13190b to your computer and use it in GitHub Desktop.
Save ShortArrow/020e9408fb79d138961152d74b13190b to your computer and use it in GitHub Desktop.
Search and listing hostnames from IPv4 with PowerShell
$network = "192.168.."
1..254 | ForEach-Object {
$ip = $network + $_
$ping = Test-Connection -ComputerName $ip -Count 1 -Quiet
if ($ping) {
try {
$hostname = ([System.Net.Dns]::GetHostByAddress($ip)).HostName
} catch {
$hostname = $_.Exception.Message
}
Write-Output "$ip - $hostname"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment