Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I'd also be happy with the non-NetBIOS name - i.e. – Mike Fiedler Jan 4 '10 at 20:28
^ That's also called the FQDN, or Fully Qualified Domain Name – Mark Henderson Jan 4 '10 at 20:40
up vote 3 down vote accepted

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.

share|improve this answer
+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 – Evan Anderson Jan 4 '10 at 23:56
Thanks, worked like a charm. Added an @ to get rid of a echoed command: @set MACHINEDOMAIN=%%f – Mike Fiedler Jan 4 '10 at 23:59
Hilarious thing about windows is that this won't work for a version in another language. – Sebastian Jun 14 '13 at 0:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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