PowerShell Tip of the Week: Get FQDN

Today I want to show you how to use .Net class to get FQDN (Fully Qualified Domain Name). A fully qualified domain name is the complete domain name for a specific computer. The FQDN consists of two parts: the hostname and the domain name like for example:

DC01.powershellbros.com

Get FQDN

In this article we will focus on DNS class which is a static class that retrieves information about a specific host from the Internet Domain Name System (DNS). For more information about this class please visit msdn site.

#Check local computer
[System.Net.Dns]::GetHostByName(($env:computerName))

#Display Fully Qualified Domain Name for local computer
([System.Net.Dns]::GetHostByName(($env:computerName))).Hostname

#Display Fully Qualified Domain Name for remote computer 
([System.Net.Dns]::GetHostByName(("ADFS01"))).Hostname
FQDN examples
FQDN examples

What if you have list of servers and you want to check them all quickly? You can combine Get-Content and Foreach commands to query them.

#Check servers from the list and display 2 properties
Get-Content -Path "C:\users\$env:username\desktop\servers.txt" | 
Foreach { "{0} - {1}" -f $_, (([System.Net.Dns]::GetHostByName(("$_"))).Hostname) }

#Check servers from the list and display 3 properties
Get-Content -Path "C:\users\$env:username\desktop\servers.txt" | 
Foreach {$DNSCheck = ([System.Net.Dns]::GetHostByName(("$_")));  "{0} - {1} - {2}" -f $_, $DNSCheck.hostname, $DNSCheck.AddressList[0]}
FQDN examples 2
FQDN examples 2

Below you can find another way to get this information from remote servers and add it to an array. Results can be saved into CSV file.

$Servers = Get-Content -Path "C:\users\$env:username\desktop\servers.txt"
$Array = @()

Foreach($Server in $Servers)
{
    $DNSCheck = $null
    $Server = $Server.trim()

    $DNSCheck = ([System.Net.Dns]::GetHostByName(("$Server")))

    $Object = New-Object PSObject -Property ([ordered]@{ 
     
                "Server name"             = $Server
                "FQDN"                    = $DNSCheck.hostname
                "IP Address"              = $DNSCheck.AddressList[0]

    })
  
    # Add object to our array
    $Array += $Object

}
$Array
$Array | Export-Csv -Path C:\users\$env:username\desktop\results.csv -NoTypeInformation

Final results will be displayed in console:

FQDN
FQDN

2 thoughts on “PowerShell Tip of the Week: Get FQDN

    1. Hi Sebastian,
      You can check it using Get-WmiObject command and add it to custom object:

      #Domain
      (Get-WmiObject win32_computersystem -ComputerName $Server).Domain

      #Script:
      $Servers = Get-Content -Path “C:\users\$env:username\desktop\servers.txt”
      $Array = @()

      Foreach($Server in $Servers)
      {
      $DNSCheck = $null
      $DomainName = $null
      $Server = $Server.trim()

      $DNSCheck = ([System.Net.Dns]::GetHostByName((“$Server”)))
      $DomainName = (Get-WmiObject win32_computersystem -ComputerName $Server).Domain
      $Object = New-Object PSObject -Property ([ordered]@{

      “Server name” = $Server
      “FQDN” = $DNSCheck.hostname
      “Domain” = $DomainName
      “IP Address” = $DNSCheck.AddressList[0]

      })

      # Add object to our array
      $Array += $Object

      }
      $Array
      $Array | Export-Csv -Path C:\users\$env:username\desktop\results.csv -NoTypeInformation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.