Get Fully Qualified Domain Name in PowerShell

PowerShell get fully qualified domain name

Using Environment Variable

Use an environment variable to get a fully qualified domain name.

First, we assigned the value of the environment variable ($evn:COMPUTERNAME) to the $computer_name variable. The environment variable COMPUTERNAME stored the current computer’s name or you can say the current host’s name; both meaning the same.

Then, we assigned the $fqdn variable with the value of the $env:USERDNSDOMAIN environment variable. Here, the USERDNSDOMAIN stored the DNS domain name, which was associated with the current user. This meant it indicated the domain name in which the user account was registered.

Finally, we joined the value of the $computer_name and $fqdn by separating them with a dot. We used the Write-Output cmdlet to display it on the PowerShell console.

Using System.Net.Dns Class

Use the System.Net.Dns class to get a fully qualified domain name.

The GetHostName() method was called using the Dns class. We used it to retrieve the local machine name, which was further assigned to the $host_name variable.

We used another method called GetHostAddresses(), providing it with the $host_name as a parameter to get an array of all IP addresses associated with the $host_name. We stored this array in the $ip_addresses variable.

Then, we used the foreach loop to loop the $ip_addresses array. In each iteration, we used the if statement with the -eq (equal to) operator to determine whether the address family (AddressFamily) of a current IP address ($current_ip) was equal to the InterNetwork, which resembled the IPv4 addresses.

The GetHostEntry() method, which required the $current_ip parameter to create a System.Net.IPHostEntry object, was utilized if the requirement was met. We used the HostName property of the System.Net.IPHostEntry object to get the FQDN, which was stored in the $fqdn variable. Next, we exit the loop using the break statement.

Why did we exit the for loop? Because we only wanted to use the first IPv4 address to get the FQDN. Lastly, we used the Write-Output cmdlet to print $fqdn on the console.

The following are the one-line solution:

According to the documentation, the GetHostByName() and Resolve() methods are deprecated ..

Using ping Command

Use the ping command to get a fully qualified domain name.

We used the ping command, which is a PowerShell command to get the local computer’s FQDN by pinging the hostname and parsing the result. How did the ping work in the above example? The ping localhost -n 1 was used to ping the local computer once to retrieve the hostname and IP address.

Then, we used the command substitution ($(...)) to capture the output of the ping localhost -n 1 command; however, the [1] got the captured output’s second line containing the hostname. Moreover, we chained the split(" ") method to split this second line into the array of words using a space as a delimiter and retrieved the second word (a hostname) by accessing the 1 index as [1].

We stored the final output in the $fqdn variable, which was further used with the Write-Output cmdlet to print its value on the PowerShell console.

If you do not access the index 1 twice as demonstrated in the above example, you will get more detailed output including the information about packets (sent, received, lost), round trip times (minimum, maximum, average), etc.

Using Get-WmiObject Cmdlet

Use the Get-WmiObject cmdlet to get a fully qualified domain name.

We started by using the $env:COMPUTERNAME environment variable to get the computer name of a local machine and saved it in the $computer_name variable. Remember, the $env:COMPUTERNAME contained the current computer’s name.

Then, we used the Get-WmiObject cmdlet to query the Windows Management Instrumentation to get details about a computer system; particularly, we targeted the Win32_ComputerSystem class of root\CIMv2 namespace, which contained different properties about computer’s operating system and hardware. We stored the details in the $wmi_obj variable. Here, the $wmi_obj represented the WMI object.

We specified the Win32_ComputerName class and 'root\CIMv2' namespace using -Class and -NameSpace parameters, respectively.

Further, we used this variable to access the value of its Domain property representing the name of an Active Directory domain to which a computer was joined. The resulting value was stored in the $domain_name variable. Finally, we used the Write-Output cmdlet to print the fully qualified domain name on the PowerShell console.

Alternatively, we can use the alias of the Get-WmiObject cmdlet as demonstrated below:

Here, we used the .DNSHostName and .Domain properties to get the local machine’s computer name and the domain to which the local computer was joined. We concatenated the both using concatenation operator (+) by separating them with a dot (.).

Using Get-CimInstance Cmdlet

Use the Get-CimInstance cmdlet to get a fully qualified domain name. Go ahead with the below solution if you’re using PowerShell 3.0+. Why? As of PowerShell 3.0, the Get-WmiObject has been superseded by Get-CimInstance. Remember, you can use Get-CimInstance on Windows platforms only.

This snippet resembles the last example learned in the previous section but we used GetCimInstance cmdlet to retrieve information about the Win32_ComputerSystem class. We used the .Name and .Domain properties to get the name of a local computer and the domain to which this computer was joined.

Using Get-ADComputer Cmdlet

Use the Get-ADComputer cmdlet to get a fully qualified domain name. You must install the Active Directory on your machine to use this cmdlet.

The above command retrieved the local computer’s DNS hostname using the Active Directory module. How did it work? We used the hostname command as $(hostname) to get the hostname of a local computer, which was specified as an argument to the Get-ADComputer cmdlet.

Then, the Get-ADComputer cmdlet accessed the information regarding a computer object in the Active Directory. Finally, we used the DNSHostName property to have the DNS hostname of the computer object. We assigned the resulting value to the $fqdn variable to further display it on the console using the Write-Output cmdlet.

Using Registry

Use the registry to get a fully qualified domain name. You can use this solution if you have installed Active Directory on your computer.

We used the Get-ItemProperty cmdlet to access the ‘HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters’ registry key to retrieve its properties. The registry key we used for this code contained network-related configuration settings.

We used the .Domain property to retrieve the domain name of the computer. This domain name is often set in the registry when a computer connects to the Active Directory domain. We stored the value of the .Domain property in the $domain_name variable.

After that, we used the $env:COMPUTERNAME to get the local computer’s name and stored it in the $computer_name variable. Next, we joined the $computer_name and $domain_name by separating them using a dot (.), and assigned the concatenated value to the $fqdn variable, which was further used with Write-Output to display it on the PowerShell console.

That’s all about how to get fully qualified domain name in PowerShell.

Was this post helpful?

Leave a Reply

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