Trying to figure out if there is an environment variable or another method to get a given machine's logon domain from simple command line script.

The variable %USERDOMAIN% will provide me with the domain my user is - and in a domain trsut scenario does not return the machine's domain, rather MY domain.

The idea is that I want to be able to have my cmd script code not care about what domain it is in, but sitll be able to determine that info at runtime.

There's VBScript method here:

Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
WScript.Echo strDomain

Another method is this:

net config workstation | findstr /C:"Workstation domain"

which outputs:

Workstation domain                   DOMAINNAME

But since there is no command line equivalent to unix cut, I am finding it difficult to get this info into a variable.

  • I'd also be happy with the non-NetBIOS name - i.e. domainname.mydomain.net Jan 4 '10 at 20:28
  • ^ That's also called the FQDN, or Fully Qualified Domain Name Jan 4 '10 at 20:40

This should do it for you:

for /f "tokens=1-3 delims= " %%d in ('net config workstation ^| findstr /c:"Workstation domain"') do set machinedomain=%%f

Then %machinedomain% will contain the domain. Note that "Workstation domain" is case-sensitive there.

  • +1 - Works for me. I'd do a "tokens=3", and I'm more of a "FIND" guy, but that's all I'd change: for /f "usebackq tokens=3" %%i in (net config workstation ^| find "Workstation d omain") do echo Domain is %%i Jan 4 '10 at 23:56
  • Thanks, worked like a charm. Added an @ to get rid of a echoed command: ...do @set MACHINEDOMAIN=%%f Jan 4 '10 at 23:59
  • 1
    Hilarious thing about windows is that this won't work for a version in another language.
    – Sebastian
    Jun 14 '13 at 0:07


wmic path win32_computersystem get domain


get-wmiobject win32_ntdomain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.