104

I am looking for a way to determine what the Name/IP Address of the domain controller is for a given domain that a client computer is connected to.

At our company we have a lot of small little networks that we use for testing and most of them have their own little domains. As an example, one of the domains is named "TESTLAB". I have an Windows XP workstation that is a member of the TESTLAB domain and I am trying to figure out the name of the domain controller so that I can go and look to see what users have been defined for the domain. In our lab there is a mix of Windows Server 2000 and Windows Server 2003 (and in reality probably a couple of NT 4 Servers) so it would be nice to find a solution that would work for both.

Looking on the Internet, it looks like there are various utilities, such as Windows Power Shell or nltest, but these all require that you download and install other utilities. I was hoping to find a way to find the domain controller without having to install anything additional.

EDIT If I wanted to write a program to find the domain controller or the users in the current domain, how would I go about doing that?

1
  • Since I spent a little time writing a solution for this, I thought I'd make it into a programming question and re-open.
    – tvanfosson
    Dec 9, 2008 at 20:34

7 Answers 7

256

With the most simple programming language: DOS batch

echo %LOGONSERVER%
3
  • Probably not what Dorky had in mind, back then, but still, probably what most of the thread's readers are looking for. eh... it WAS what Dorky was looking for. Hah! Dec 7, 2011 at 14:46
  • 3
    This won't work if you're looking for a DC in another Domain in which the machine doesn't belong. Nov 13, 2012 at 13:21
  • @DorkyEngineer how about marking this as your selected answer?
    – ErikE
    Jan 4, 2013 at 2:44
20

In cmd on Windows, type the following commande:

nltest /dclist:{domainname}

It lists all domain controllers in particular domain

1
  • 3
    and nltest /dclist: lists all the controllers on any domain my laptop is connected to (Windows 7)
    – GMasucci
    Aug 16, 2016 at 15:35
14

In C#/.NET 3.5 you could write a little program to do:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    string controller = context.ConnectedServer;
    Console.WriteLine( "Domain Controller:" + controller );
} 

This will list all the users in the current domain:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    using (UserPrincipal searchPrincipal = new UserPrincipal(context))
    {
       using (PrincipalSearcher searcher = new PrincipalSearcher(searchPrincipal))
       {
           foreach (UserPrincipal principal in searcher.FindAll())
           {
               Console.WriteLine( principal.SamAccountName);
           }
       }
    }
}
0
10

From command line query the logonserver env variable.

C:> SET L

LOGONSERVER='\'\DCNAME

0
7

Run gpresult at a Windows command prompt. You'll get an abundance of information about the current domain, current user, user & computer security groups, group policy names, Active Directory Distinguished Name, and so on.

4
  • 1
    I had to use the argument "/R" to get the report.
    – Josiah
    Sep 22, 2015 at 21:06
  • 1
    You should try gpresult /Z!!
    – ErikE
    Sep 22, 2015 at 21:23
  • What is the advantage of /Z? For the purpose of determining the DC in use, they seem to be comparable.
    – Josiah
    Sep 23, 2015 at 22:43
  • 1
    People might like to be directed to the option because of the wealth of information available. But you're right, for JUST the DC in use, other options are more succinct.
    – ErikE
    Sep 23, 2015 at 22:45
6

in Powershell: $env:logonserver

0
1

To retrieve the information when the DomainController exists in a Domain in which your machine doesn't belong, you need something more.

  DirectoryContext domainContext =  new DirectoryContext(DirectoryContextType.Domain, "targetDomainName", "validUserInDomain", "validUserPassword");

  var domain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(domainContext);
  var controller = domain.FindDomainController();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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