Table of Contents
Using Environment Variable
Use an environment variable to get a fully qualified domain name.
1 2 3 4 5 |
$computer_name = $env:COMPUTERNAME $fqdn = $env:USERDNSDOMAIN Write-Output "Fully Qualified Domain Name: $computer_name.$fqdn" |
1 2 3 |
Fully Qualified Domain Name: MAS-DC-01.MASLAB.COM |
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.
1 2 3 4 5 6 7 8 9 10 11 |
$host_name = [System.Net.Dns]::GetHostName() $ip_addresses = [System.Net.Dns]::GetHostAddresses($host_name) foreach ($current_ip in $ip_addresses) { if ($current_ip.AddressFamily -eq 'InterNetwork') { $fqdn = [System.Net.Dns]::GetHostEntry($current_ip).HostName break } } Write-Output "Fully Qualified Domain Name: $fqdn" |
1 2 3 |
Fully Qualified Domain Name: MAS-DC-01.maslab.com |
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:
1 2 3 4 5 |
[System.Net.Dns]::GetHostEntry($env:COMPUTERNAME).HostName #OR [System.Net.Dns]::GetHostEntry([string]"localhost").HostName |
1 2 3 4 |
MAS-DC-01.maslab.com MAS-DC-01.maslab.com |
According to the documentation, the
GetHostByName()
andResolve()
methods are deprecated ..
Using ping
Command
Use the ping
command to get a fully qualified domain name.
1 2 3 4 |
$fqdn= $(ping localhost -n 1)[1].split(" ")[1] Write-Output "Fully Qualified Domain Name: $fqdn" |
1 2 3 |
Fully Qualified Domain Name: MAS-DC-01.maslab.com |
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.
1 2 3 4 5 6 |
$computer_name = $env:COMPUTERNAME $wmi_obj = Get-WmiObject -Class Win32_ComputerSystem -Namespace 'root\CIMv2' $domain_name = $wmi_obj.Domain Write-Output "Fully Qualified Domain Name: $computer_name.$domain_name" |
1 2 3 |
Fully Qualified Domain Name: MAS-DC-01.maslab.com |
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:
1 2 3 |
(gwmi -Class Win32_ComputerSystem).DNSHostName+"."+(gwmi -Class Win32_ComputerSystem).Domain |
1 2 3 |
MAS-DC-01.maslab.com |
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.
1 2 3 |
(Get-CimInstance -Class Win32_ComputerSystem).Name+"."+(Get-CimInstance -ClassWin32_ComputerSystem).Domain |
1 2 3 |
MAS-DC-01.maslab.com |
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.
1 2 3 4 |
$fqdn = (Get-ADComputer $(hostname)).DNSHostName Write-Output "Fully Qualified Domain Name: $fqdn" |
1 2 3 |
Fully Qualified Domain Name: MAS-DC-01.maslab.com |
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.
Further reading:
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.
1 2 3 4 5 6 |
$domain_name = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters').Domain $computer_name = $env:COMPUTERNAME $fqdn = "$computer_name.$domain_name" Write-Output "Fully Qualified Domain Name: $fqdn" |
1 2 3 |
Fully Qualified Domain Name: MAS-DC-01.maslab.com |
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.