DevOps | Scripts | Automation

CMDPowershellPython

How to get the FQDN of the Windows server?

There are several methods to get the fQDN (Fully Qualified Domain Name) of the Server. Here we will discuss a few known methods.

Methods:

  • Using Computer Properties.
  • Using PowerShell
  • Using Python
  • Using Cmd
1. Using Computer Properties

A straightforward method to check the FQDN of the server is by clicking right-click on “This PC” and selecting properties. This is a rapid method.

Computer Properties
Server FQDN

Here, you can get the domain name as well as the FQDN of the server. But this method doesn’t work if you want to get the multiple servers’ computer properties remotely. To get FQDN for multiple servers, you need to use scripting methods as discussed below.

2. Using PowerShell script

There are several ways in PowerShell to get the FQDN. You can combine the HostName and the domain name to get the FQDN. For example,

$env:COMPUTERNAME + "." + $env:USERDNSDOMAIN
DNS Domain

The other way to get the FQDN of the host is with the below command.

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

If you need to get the FQDN for multiple remote computers then use the below script from GitHub.

https://github.com/chiragce17/MyPublicRepo/blob/main/PowerShell/Get-ServerFQDN/Get-ServerFQDN.ps1

3. Using Python

Using python, you can also get the FQDN of the system and for that, you need to import the socket module.

import socket
fqdn = socket.getfqdn()
print("Server FQDN: " + fqdn)
4. Using cmd

Using the command prompt, you can also fetch the Server FQDN. For that, you need to use the environment variable.

The below command is shown for the local computer.

echo %COMPUTERNAME%.%USERDNSDOMAIN%

In the above command %COMPUTERNAME% is the environment variable name and %USERDNSDOMAIN% is the machine domain it is joined to, and the echo command is to print the variable value. The output of the above command will be like,

FQDN using cmd

To get the FQDN remotely using cmd, you can use psexec utility. PSTools you can download from the Microsoft site.

https://learn.microsoft.com/en-us/sysinternals/downloads/psexec

You can also download PStools from my git repository.

https://github.com/chiragce17/MyPublicRepo/tree/main/Utilities/PSTools

Once downloaded, browse that path using cmd and run the below command. It will get the Server FQDN from 2 servers (Server2 and AdServer).

psexec \\server2,adserver cmd.exe /c echo ^%computername^%.%userdnsdomain%

Output:

Remote Servers FQDN using cmd