How can I list all domain controllers (DCs) for an Active Directory (AD) domain?

A. The following script, which you can download here , accepts a domain as its parameter, then lists all DCs in that domain. Some lines wrap because of space constraints.


If Wscript.Arguments.Count required. For example:" & vbCrLf _ & "cscript listdcs.vbs dc=savilltech,dc=com" Wscript.Quit(0) End If

strDomainName = Wscript.Arguments(0)

Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCOmmand.ActiveConnection = objConnection

objCommand.CommandText = _ "Select distinguishedName from " & _ "'LDAP://cn=Configuration," & strDomainName & "' " _ & "where objectClass='nTDSDSA'" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst

Do Until objRecordSet.EOF DCDN = objRecordSet.Fields("distinguishedName").Value DomainController = Mid(DCDN,21,InStr(Mid(DCDN,21),",")-1) Wscript.Echo "Domain Controller: " & DomainController objRecordSet.MoveNext Loop

This sample execution produced the following output when I ran it on my system:

D:\Temp>cscript listdcs.vbs dc=savilltech,dc=com

Domain Controller: SAVDALDC01
Domain Controller: SAVDALDC02
Domain Controller: SAVLONDC01
Domain Controller: SAVDALLABDC01

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.