≡ Menu

Powershell: Get FQDN of local or remote computer

Have you ever wondered how to get FQDN of local or remote computer(Workstations/Server) using powershell? There are some methods available over internet but they are not really upto the mark. All they do is, pick computer name and domain name from two places and concatenate them to form a FQDN. But as a System administrator I can say that is wrong approach and possibility is there where computers can have different domain name and FQDN names. For example, I can configure my computer which is part of techibee.com domain to have a FQDN as mytestpc1.intranet.techibee.com. If such cases, joining the computer name and domain name simply won’t help.

After my research, I felt below procedure is very standard approach compared to any other options.

To get FQDN of local computer:

[System.Net.Dns]::GetHostByName(($env:computerName))

To get FQDN of Remote computer:

[System.Net.Dns]::GetHostByName("mytestpc1")

The System.Net.DNS class has a few other useful methods using which you can get FDQN and IP address details. Refer to http://msdn.microsoft.com/en-us/library/system.net.dns.aspx for more details.

PS C:\> [System.Net.Dns] | Get-Member -Static

TypeName: System.Net.Dns

Name                  MemberType Definition
—-                  ———- ———-
BeginGetHostAddresses Method     static System.IAsyncResult BeginGetHostAddr…
BeginGetHostByName    Method     static System.IAsyncResult BeginGetHostByNa…
BeginGetHostEntry     Method     static System.IAsyncResult BeginGetHostEntr…
BeginResolve          Method     static System.IAsyncResult BeginResolve(str…
EndGetHostAddresses   Method     static ipaddress[] EndGetHostAddresses(Syst…
EndGetHostByName      Method     static System.Net.IPHostEntry EndGetHostByN…
EndGetHostEntry       Method     static System.Net.IPHostEntry EndGetHostEnt…
EndResolve            Method     static System.Net.IPHostEntry EndResolve(Sy…
Equals                Method     static bool Equals(System.Object objA, Syst…
GetHostAddresses      Method     static ipaddress[] GetHostAddresses(string …
GetHostByAddress      Method     static System.Net.IPHostEntry GetHostByAddr…
GetHostByName         Method     static System.Net.IPHostEntry GetHostByName…
GetHostEntry          Method     static System.Net.IPHostEntry GetHostEntry(…
GetHostName           Method     static string GetHostName()
ReferenceEquals       Method     static bool ReferenceEquals(System.Object o…
Resolve               Method     static System.Net.IPHostEntry Resolve(strin…

PS C:\>

Hope this helps and happy learning..

Comments on this entry are closed.

  • PWIDINC January 11, 2015, 9:27 pm

    Good post. Thanks!

  • Jason Fisher November 14, 2017, 9:00 pm

    Thanks for this, it worked well through an SCCM package, some other methods do not work in SCCM.