Here is the simple powershell script gets the FQDN of remote computers. If you are a Widows administrator and working for a large enterprise you must have encounter a situation to pull FQDN of multiple remote computers.
In this article we explain how to pull remote computer FQDN, find the script code and it’s usage below.
Powershell get FQDN of Local Computer
[System.Net.Dns]::GetHostByName(($env:computerName))
The above simple one line code gets your local computer FQDN
Powershell script to get FQDN of multple remote computers
We need to create a file for storing all the servers or multiple machines list, I named it as Allservers.txt for and saved it in my local machine c:\temp\Allservers.txt
$allservers = Get-Content -path "c:\temp\allservers.txt"
#Looping each server
Foreach($Server in $allservers)
{
$fqdn = Invoke-Command -ComputerName $server -Scriptblock {[System.Net.Dns]::GetHostByName(($server))}
write-host "The FQDN of Server" $server "$fqdn.HostName
}
# End of script
# one can also try this code no difference in ouput
$allservers = Get-Content -path "c:\temp\allservers.txt"
#Looping each server
Foreach($Server in $allservers)
{
Invoke-Command -ComputerName $server -Scriptblock {[System.Net.Dns]::GetHostByName(($env:computerName))}
}
# End of script
One can also retrieve the IP address and FQDN of remote computers, here the code looks like.
# one can also try this code no difference in ouput
$allservers = Get-Content -path "c:\temp\allservers.txt"
#Looping each server
Foreach($Server in $allservers)
{
$fqdn = Invoke-Command -ComputerName $server -Scriptblock {[System.Net.Dns]::GetHostByName(($server))}
write-host "The FQDN of Server" $server "$fqdn.HostName
write-host "The FQDN of " $server "Server" $fqdn.HostName "and IP Address" $fqdn.AddressList.IPAddressToString
}
# End of script
One should have admin rights to execute this script on remote computers, generally, system administrators have admin access to computers, if not one will run into permission or access denied issues, make sure you have admin rights on the remote computers.
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply