Home > PowerShell > Get the FQDN of your host with PowerShell

Get the FQDN of your host with PowerShell

I was looking into a way to get the FQDN of my server in PowerShell.  There are some Environment Variables (like USERDNSDOMAIN), but they reflect the current logged on user and not the computer.

 

Use the following code to list your FQDN:

$objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()

"{0}.{1}" -f $objIPProperties.HostName, $objIPProperties.DomainName

Done!

Advertisements
Categories: PowerShell
  1. September 2, 2011 at 16:03

    A much easier way to get the FQDN.

    # Get FQDN
    $sysinfo = Get-WmiObject -Class Win32_ComputerSystem
    $fqdn = “{0}.{1}” -f $sysinfo.Name, $sysinfo.Domain

    Easy Peasy

    • April 21, 2012 at 09:28

      Use this to get the correct FQDN. Workgroup systems do not have a DNS suffix; the above scripts show compname.WORKGROUP in these instances. This is crucial when using to generate the subject name for an x509 PKI cert.

      $objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
      if (!($objIPProperties.DomainName -eq $null)) {
      “{0}” -f $objIPProperties.HostName
      }
      else {
      “{0}.{1}” -f $objIPProperties.HostName, $objIPProperties.DomainName
      }

  2. April 21, 2012 at 09:31

    Use this to get true FQDN for a system. Workgroup systems potentially are not registered in DNS. The above scripts show such systems as computername.WORKGROUP . This could cause issues if using to generate the subject name for an x509 cert, for instance

    $objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
    if (!($objIPProperties.DomainName -eq $null)) {
    “{0}” -f $objIPProperties.HostName
    }
    else {
    “{0}.{1}” -f $objIPProperties.HostName, $objIPProperties.DomainName
    }

    • April 21, 2012 at 09:37

      Oops, correct version !

      $objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
      if ($objIPProperties.DomainName -eq $null) {
      “{0}” -f $objIPProperties.HostName
      }
      else {
      “{0}.{1}” -f $objIPProperties.HostName, $objIPProperties.DomainName
      }

  3. August 21, 2012 at 07:17

    Using [System.Net.DNS] class is more reliable method to get DNS name of a computer.

    http://techibee.com/powershell/powershell-get-fqdn-of-local-or-remote-computer/1742

  4. laderio
    December 14, 2012 at 13:01

    Sorry, but Name + Domain is not the FQDN of the Computer.

    • laderio
      December 14, 2012 at 13:15

      Here is an example, how you can modify the FQDN without changing the domain:
      http://web.urz.uni-heidelberg.de/windows/vista/vista-domaene.html

      • December 14, 2012 at 21:09

        In this example, you’re changing the Primary DNS Suffix. This means that this suffix will be added to every DNS lookup when you only provide the hostname.

        But in most cases, the primary DNS suffix matches the domain name…

      • laderio
        December 15, 2012 at 15:23

        Sorry, but it is not 😉

        You have 3 FQDNs for a computer in a domain:
        One in Active Directory. Here it also must not be NetBIOS-Name + Domain Suffix. It can be different.
        Second one is in DNS. Here too you can have a different FQDN then NetBIOS-Name + Domain Name.
        Third is on the computer itself. When you look for the link i have provided, you can see, that FQDN of the computer can be diffferent from NetBIOS-Name + Domain Name.

        I was working on a script comparing this 3 FQDNs for our servers and careless adoptet the code from here. But then I’ve noticed, that this was a mistake, because the Script here does not provide FQDN of the computer, but NetBIOS-Name + Domain Name. But this is not, what is the FQDN in all cases, so I had to change the code, see my Post with the correct solution.

        Also your statement is wrong, that this suffix is added to every lookup. Also this depends on the configured lookup-method, as you can provide a different Suffix-Search-List.

        Where I am not sure is, if this is not also an issue with Kerberos. I only know, that here the domain FQDN and the computer FQDN are relevant.

      • December 17, 2012 at 17:11

        Didn’t realize that!

        Thanks for the info!

        Honestly, in most scenario’s that i see, FQDN & NetBIOS+DomainName matches. But indeed, for those other cases, you might want to use some other Powershell commands to fetch the exact info you need.

  5. laderio
    December 14, 2012 at 15:57

    Here is the correct solution for the domain:
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $Computer)
    $regKey= $reg.OpenSubKey(“SYSTEM\CurrentControlSet\Services\Tcpip\Parameters”)

  6. Rob Alexander
    January 2, 2013 at 19:45

    Sorry, I don’t do this in Powershell so you’ll have to write your own code, but this page was helpful in figuring out the logical process so I thought I’d pass it on.

    What you want to do for this is:
    First get DNS host name (not NetBIOS name) from HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname

    Then get domain suffix. First try querying: HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Domain

    If that’s NULL, then cycle through each IPEnabled NIC via Win32_NetworkAdapterConfiguration, (SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True), checking the “DNSDomain” property until you find one that’s not NULL.

    This should generally work to get FDQN for properly configured corporate systems whether joined to an AD domain or in a workgroup, assuming that if it’s in a workgroup, the primary DNS suffix was set in the adapter’s TCP/IP properties, and works in multi-homed systems where backups go over a separate network (like ours do).

  1. March 29, 2012 at 23:24
  2. November 26, 2012 at 15:35

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: