I have scipted ForestRootDomain.bat to determine the forest root domain in a batch.

The syntax for using ForestRootDomain.bat is:

call ForestRootDomain VAR \[/DN\]

where VAR is a call directed environment variable that will contain the forest root domain, and /DN is an optional switch which returns the distinguished name of the forest root domain in VAR.

Example:

When is use call ForestRootDomain VAR, the %VAR% variable is returned as JSIINC.COM.

When is use call ForestRootDomain VAR /DN, the %VAR% variable is returned as DC=JSIINC,DC=COM.

ForestRootDomain.bat contains:

@echo off                              if \{%1\}

\{\} @echo Syntax Call ForestRootDomain VAR \[/DN\]&exit /b 1 setlocal @echo Set objSysInfo = CreateObject("ADSystemInfo")>"%TEMP%\ForestRootDomain.vbs" @echo Wscript.Echo "Forest DNS Name: " ^& objSysInfo.ForestDNSName>>"%TEMP%\ForestRootDomain.vbs" if /i not \{%2\}

\{/dn\} goto run @echo Set objRootDSE = GetObject("LDAP://RootDSE")>"%TEMP%\ForestRootDomain.vbs" @echo Wscript.Echo "Root Domain: " ^& objRootDSE.Get("RootDomainNamingContext")>>"%TEMP%\ForestRootDomain.vbs" :run for /f "Tokens=1* Delims=:" %%c in ('cscript //Nologo "%TEMP%\ForestRootDomain.vbs"') do set frd=%%d set frd=%frd:~1% del /q "%TEMP%\ForestRootDomain.vbs" endlocal&set %1=%frd% exit /b 0