Determine machine's logon domain from command line

29,014

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:
29,014

Related videos on Youtube

Mike Fiedler
Author by

Mike Fiedler

Been playing with systems for some time now, all platforms, all tastes and languages. Finding the right tool for the job is always the best part of a challenge, and seeing it work is amazing. Figuring out how a complex system works, and how it should work is even better.

Updated on September 17, 2022

Comments

  • Mike Fiedler
    Mike Fiedler over 1 year

    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.

    • Admin
      Admin over 14 years
      I'd also be happy with the non-NetBIOS name - i.e. domainname.mydomain.net
    • Admin
      Admin over 14 years
      ^ That's also called the FQDN, or Fully Qualified Domain Name
  • Spence
    Spence over 14 years
    +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
  • Mike Fiedler
    Mike Fiedler over 14 years
    Thanks, worked like a charm. Added an @ to get rid of a echoed command: ...do @set MACHINEDOMAIN=%%f
  • Sebastian
    Sebastian almost 11 years
    Hilarious thing about windows is that this won't work for a version in another language.