How to get domain name in Windows using command?





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

systeminfo | findstr /B /C:”Domain”

You can run below command on command prompt:

set user

It gives you a lot more information related to domain in addition to the name of domain as shown in below snapshot:

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

enter image description here

Important Note: Domain on which your computer is registered might not be same as the domain on which the logged-in user is registered. Please read about transitivity and direction in domain trust to know how a user registered in one domain can login to a computer on another domain.

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%