I want to retrieve the FQDN name of windows server via powershell script. I have found 2 solution so far:
$server = Invoke-Command -ScriptBlock {hostname}
Above line will print just the short name of the server
$sysinfo = Get-WmiObject -Class Win32_ComputerSystem $server = “{0}.{1}” -f $sysinfo.Name, $sysinfo.Domain
Above two line will get me the FQDN but this looks really nasty code to retrieve just the hostname :(
So, My question is, is there an easier way to get the FQDN in powershell. I am a bash/perl coder and recently picked up powershell.. so finding it difficult.
Thanks.
On the Windows Taskbar, click Start > Programs > Administrative Tools > Active Directory Domains and Trusts. In the left pane of the Active Directory Domains and Trusts dialog box, look under Active Directory Domains and Trusts. The FQDN for the computer or computers is listed.
Type "ipconfig" and press "Enter." This displays the IP address for your Windows server. Use this IP address to view the fully qualified domain name of the server.
To get FQDN of local computer:
[System.Net.Dns]::GetHostByName($env:computerName)
or
[System.Net.Dns]::GetHostByName($env:computerName).HostName
To get FQDN of Remote computer:
[System.Net.Dns]::GetHostByName('mytestpc1')
or
For better formatted value use:
[System.Net.Dns]::GetHostByName('mytestpc1').HostName
How about: "$env:computername.$env:userdnsdomain"
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 doesn't work with disjointed name space AD configurations.
Use this as referenced in another answer:
$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain ; Write-Host $myFQDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With