To retrieve the FQDN name of windows server via powershell script.

This actually only works if the user is logged into a domain (i.e. no local accounts), logged into the same domain as the server, and this doesn't work with disjointed name space.

Example

#computerName
[System.Net.Dns]::GetHostByName(($env:computerName))
#Hostname
([System.Net.Dns]::GetHostByName(($env:computerName))).Hostname
#Other remote computer Hostname
([System.Net.Dns]::GetHostByName(("XYZ-07"))).Hostname

Copy and Try it

Another way is

Example

$localFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
$localFQDN

Copy and Try it

Example

write-host $env:computername
write-host $env:userdnsdomain

Copy and Try it