45

Is there a way to find the fully qualified domain name of a Windows XP box?

Being unfamiliar with Windows I would describe what I'm looking for as the equivalent of the command hostname --fqdn available in Linux.

12 Answers 12

26

You can find it in the system properties ("Computer name" tab).

With the command line, you can run IPCONFIG /ALL and have a look at the "Host name" and "Primary DNS suffix" fields.

6
  • The two different ways give different results. ipconfig gives what I was expecting, but in the 'Computer Name' tab, the domain is different. It looks like a Workgroup name rather than a domain suffix. Is this a Windows specific thing where they are somehow equivalent? Oct 12, 2009 at 13:00
  • In the "Computer name" tab, you should look at the "Full computer name" value; the "domain" value is the Windows domain (or workgroup) the computer is member of.
    – Massimo
    Oct 12, 2009 at 13:23
  • Ah right. Well there must be something wrong with the install on this machine, as it's only showing the hostname (with a dot at the end). Probably wouldn't have had to ask the question if it had been showing it as expected :-D. +1 Oct 12, 2009 at 13:28
  • Have you tried clicking on "Change" and then "More"?
    – Massimo
    Oct 12, 2009 at 13:42
  • 1
    Just remember some systems are localized so looking for the proper line by nr may be better in some cases (it's line 5 afaik).
    – RnR
    Jul 20, 2017 at 11:10
37

There is no such option to the hostname command in windows. However, this should do the trick:

echo %COMPUTERNAME%.%USERDNSDOMAIN%

Or you can grep (under Windows: find /I "string") for Host- and Domain from set or systeminfo or ipconfig -all name and glue it together elsewhere.

Edit: fixed Typo. Thanks Benoit

Update: The variable %USERDNSDOMAIN% is only available when logged on to a domain... The DNS suffix you get from a DHCP server is not put into a environment variable (as far as I could figure out).

5
  • 1
    The second variable is "%USERDNSDOMAIN%".
    – Benoit
    Oct 12, 2009 at 13:36
  • %USERDNSDOMAIN% doesn't appear to be a variable on this system. Unless I'm doing something wrong: H:\>echo %COMPUTERNAME%.%USERDNSDOMAIN% [correctname].%USERDNSDOMAIN% Oct 12, 2009 at 14:38
  • Ach, forgot that pasting that would be horrible in the comments. Oct 12, 2009 at 14:39
  • 2
    You can use WMIC to retrieve domain name: wmic computersystem get domain
    – Noam Manos
    Apr 15, 2015 at 7:14
  • 1
    This works only if the user is in the same domain like the computer. I suggest NOT to use %USERDNSDOMAIN% for the FQDN of the computer. Mar 9, 2021 at 11:59
18

The command is:

ping -a localhost
2

Try this from the command prompt:

FOR /F "tokens=2" %i in ('systeminfo ^| find /i "Domain"') do echo %computername%.%i

remember to use double % for %i if using this in a batchfile. e.g. %%i

A reason you may want to do it this way is: if your users and computers are in different domains, the %USERDNSDOMAIN% will not be correct when applied to your computer. If you only have one domain and no child domains, then you can use the other solutions above if you like.

1

vbscript :

' Print FQDN in lower case letters
' Volker Fröhlich (2011)

option explicit
dim Message
dim output
dim WshShell, objEnv
dim mydomain

' Read value from registry
function readFromRegistry (strRegistryKey, strDefault )
    Dim WSHShell, value

    On Error Resume Next
    Set WSHShell = CreateObject("WScript.Shell")
    value = WSHShell.RegRead( strRegistryKey )

    if err.number <> 0 then
        readFromRegistry= strDefault
    else
        readFromRegistry=value
    end if

    set WSHShell = nothing
end function

mydomain = readfromRegistry("HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Domain", "asdf")

' Get the WshShell object
Set WshShell = CreateObject("WScript.Shell")

' Get collection by using the Environment property
Set objEnv = WshShell.Environment("Process")

if (mydomain="") then
Message = LCase(objEnv("COMPUTERNAME"))
else
Message = LCase(objEnv("COMPUTERNAME")) & "." & mydomain
end if

' Write to stdout
set output = wscript.stdout
output.writeline Message

DOS BATCH FILE TO CALL ABOVE SCRIPT :

for /f %%a in ('cscript //nologo yourscriptname.vbs') do set FQDN=%%a
echo %FQDN%
pause
1

This will also work and does not have the delay of systeminfo:

for /f "tokens=2 delims=:" %i in ('ipconfig /all ^| findstr Search ') do SET domain=%i & SET newdomain=%domain: =% & echo %COMPUTERNAME%.%newdomain%

1
  • 1
    Use four leading spaces to format your answer as 'code'. The ubiquitous 'help' link is actually helpful.
    – Jeter-work
    May 11, 2017 at 17:52
1

Another version:

echo.
echo Getting FQDN...
FOR /F "tokens=1-2" %%A in ('ping -a localhost -n 1') do (
    echo %%A | find /i "Pinging" >nul
    IF NOT ERRORLEVEL 1 SET "FQDN=%%B"
)
echo %FQDN%
1

With PowerShell:

(gpv HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters HostName, Domain) -join '.'
0

If you need to port Unix shell scripts to windows or just like to work on the CLI, have a look at GNUwin32. It provides the common tools like cut, grep, etc for Windows.

0

Here is a CMD script for this:

@ECHO OFF

FOR /f "tokens=2,* delims= " %%a in ('IPCONFIG ^/ALL ^| FINDSTR "Primary Dns"') do set tempsuffix=%%b
FOR /f "tokens=1,2 delims=:" %%a in ('echo %tempsuffix%') do set dnssuffix=%%b
SET FQDN=%COMPUTERNAME%.%DNSSUFFIX:~1%

ECHO Server FQDN: %FQDN%
0

use 'hostname' and combine with for /F "tokens=3" %%I in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v Domain') do set _MyDomain=%%I

1
  • 1
    can you please update the question due it is really poor in quality even if it may answer the question
    – djdomi
    Nov 6, 2021 at 18:08
0

An other way in PowerShell

Write-Host "$(Hostname).$((Get-NetIPConfiguration).NetProfile.Name)"

Or as a variable

$FQDN = $(Hostname).$((Get-NetIPConfiguration).NetProfile.Name)
$FQDN

Sample output

ServerName.ad.example.com

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .