In tip 8241, I showed how NETDOM and DSQUERY SERVER returns a list of domain controllers.

Using Nltest.exe from the Support / Tools folder of your operating system CD-ROM, I have scripted GetDCs.bat to return the NetBIOS computer name of the domain controllers in a domain.

The syntax for using GetDCs.bat is:

GetDCs \[DomainName\]

Where DomainName is an optional parameter that specifies either the NetBios or DNS domain name you wish to query. If DomainName is NOT specified, the domain that you are logged onto will be used.

The output is displayed on the console, but you can redirect it to a file using GetDCs \[DomainName\]>FileName, or you can process the output using a command like:

for /f "Tokens=*" %%d in ('GetDCs') do (
 if not exist "\\%%d\netlogon" goto :EOF
  for /f "Tokens=*" %%a in ('dir "\\%%d\netlogon" /b') do (
   @echo %%d %%a
 )
)
GetDCs.bat contains:
@echo off
setlocal
set DOMAIN=%USERDNSDOMAIN%
if not \{%1\}==\{\} set DOMAIN=%1
for /f "Tokens=1 Delims=. " %%d in ('nltest /dclist:%DOMAIN%^|Findstr /C:"Site:"') do (
 @echo %%d
)
endlocal