43

I need a similar command to Linux' domainname on Windows without using any third-party application.

Is there such a command?

1
  • 1
    For information - domain name of computer and domain name of the logged-in user might not be same. Commented Feb 11, 2019 at 9:17

9 Answers 9

46

Try:

echo %USERDOMAIN% 

or

echo %USERDNSDOMAIN% 

If that still doesn't work, you can try using systeminfo:

systeminfo | findstr /B /C:"Domain" 
1
12

You can run below mentioned command on command prompt to get the desired information:

set user 

It gives us lot more information in addition to domain name (Refer screenshot):

enter image description here

List of details returned by the command are as below:

  1. User DNS Domain
  2. User Domain
  3. User Domain Roaming Profile
  4. User Name
  5. User Profile

Important Note: Domain in which your computer is registered might not be the same as the one in which the logged-in user is registered. Reader is encouraged to explore - transitivity and direction in domain trust. This will help you to know about how a user registered in one domain can login to a computer registered in a different domain.

1
  • gci env:USER* is the PowerShell equivalent - handy when cmd.exe is blocked. Though in strange order for whatever reason: on one PC I get USERDNSDOMAIN USERPROFILE USERNAME USERDOMAIN, on another one USERPROFILE USERNAME USERDOMAIN_ROAMINGPROFILE USERDOMAIN USERDNSDOMAIN. See also here, SO and serverfault Commented Jun 2 at 11:22
6

I just want to add my own solution just in case someone needs a fast way to do it.

My approach is using registry to get domain info:

reg query "HKLM\System\CurrentControlSet\Services\Tcpip\Parameters" /v "Domain" 
2

The %USERDOMAIN% and the network computer domain can be different. The systeminfo command will get the right answer but it is SLOW! Here is a solution I've used:

@REM + find the computer domain name FOR /F "usebackq tokens=*" %%a IN (`ipconfig /all`) DO ( @((ECHO %%a | findstr /i /c:"Primary Dns Suffix") && SET _str=%%a) > NUL 2>&1 ) FOR /F "tokens=2 delims=:" %%a IN ("%_str%") do SET _computerDomain=%%a SET _computerDomain=%_computerDomain: =% SET _fqdn=%COMPUTERNAME%.%_computerDomain% 
2

@Mike: fine solution - but I had some problems with it in a multi-language enviroment. I have German and English servers.

I changed your script to use wmic.exe:

@REM + Find the computer domain name @echo off FOR /F "usebackq tokens=*" %%a IN (`wmic.exe COMPUTERSYSTEM GET DOMAIN /Value`) DO ( @((ECHO %%a | findstr /i /c:"Domain=") && SET _str=%%a) > NUL 2>&1 ) FOR /F "tokens=2 delims=^=" %%a IN ("%_str%") do SET _computerDomain=%%a SET _computerDomain=%_computerDomain: =% SET _fqdn=%COMPUTERNAME%.%_computerDomain% echo %_fqdn% 

Thx for your idea

1

One line is enough to get the domain using a local user:

FOR /F "usebackq tokens=2 delims==" %%a IN (`wmic.exe COMPUTERSYSTEM GET DOMAIN /Value ^|find /i "domain"`) DO set _computerDom=%%a 
1

Most of the answers here aren't quite right. The settings mentioned are for the configured domain, however that may not be the domain the computer is actually on. The domain changes depending on what the computer is connected to - one WiFi connection may go to one domain, and a different one to another. Then connecting on VPN will put you in yet another domain. In all the cases the registry setting shown, and the USERDOMAIN and USERDNSDOMAIN environment variable values will remain unchanged.

To get the actual domain on which the computer is connected - use the "ipconfig" command line command, and see the "Connection-specific DNS Suffix" value. Note, for instance that if you disconnect or disable your connection, that will go away, indicating (correctly) that you are no longer in any domain.

A computer can, in fact, be connected on multiple domains, even, by connecting multiple ethernet ports, wifi ports, etc. to different places.

1

Just to throw another into the pot, for an ActiveDirectory domain-joined machine (not user), using ADSI (via .NET):

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().name 

This may not be the same as the DNS domain the machine is using.

0

Tip of the hat to LeeM for his input on Active Directory vs. DNS domain names and registry locations.

Preface:

  • The Linux domainname utility reports the NIS/YP domain name, though a dnsdomainname utility for reporting the DNS one also exists.

  • By analogy, the Windows equivalent to the platform-native NIS/YP domain name would be the AD (Active Directory) domain name, which, as LeeM notes, may or may not be the same as the DNS one.

  • The solutions below cover both.


To offer PowerShell solutions:

To obtain the AD domain name, use CIM (WMI):

# AD domain name only. (Get-CimInstance Win32_ComputerSystem).Domain # FQDN (fully qualified domain name, including hostname) ($cs = Get-CimInstance Win32_ComputerSystem).DNSHostName + '.' + $cs.Domain 

Note:

  • On non-domain-joined machines, the DNS domain is reported as WORKGROUP

  • Get-CimInstance calls are versatile, but comparatively slow; LeeM's own answer shows a faster .NET API alternative; note that it throws an exception (statement-terminating error) on non-domain machines.


To obtain the DNS domain name, using a .NET API, which also works on non-Windows platforms:

# DNS domain name only. [System.Net.Dns]::GetHostEntry('').HostName -replace '^[^.]+\.?' # FQDN (fully qualified domain name, including hostname) [System.Net.Dns]::GetHostEntry('').HostName 

Note:

  • On non-domain-joined machines, the DNS domain is effectively the empty string.

  • The DNS domain name (and hostname) is also stored in the Windows registry and can alternatively be retrieved as follows (also returns the empty string on non-domain machines):

    # Note: 'gpv' is a built-in alias of 'Get-ItemPropertyValue' gpv HKLM:\System\CurrentControlSet\Services\Tcpip\Parameters Domain 
4
  • CIM is painfully slow for this - the registry method given previously is way faster (OK, milliseconds, but still). If you want the powershelly version: Get-ItemPropertyValue -Path 'HKLM:\System\CurrentControlSet\Services\Tcpip\Parameters' -Name 'Domain' Commented Feb 5 at 5:05
  • @LeeM: Use of dedicated APIs is preferable to relying on implementation details such as registry paths. If the performance of the CIM command is of concern, the .NET API solution can be used. Commented Feb 5 at 12:37
  • What I didn't note was that your answer is conflating two different values. The Domain property in Win32_ComputerSystem is the Active Directory domain or workgroup that the machine is a member of. [System.Net.Dns] (and the reg key, which is still valid to query, although [System.Net.Dns] is "nicer") is listing the DNS domain of the machine. This may or may not be the same as the AD domain. The Linux domainname command actually lists the machine's DNS domain/zone (it's definitely possible the OP wants the AD domain). My answer didn't clarify that either, to be fair. Commented Feb 6 at 6:35
  • I appreciate the info, @LeeM. I've incorporated your feedback into the answer; if you're up for it, let us know if it is correct / complete now. Commented Feb 6 at 13:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.