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\}<h1><a name="_echo_Syntax_Call_ForestRootDomain_VAR_DN_amp_exit_b_1_setlocal_echo_Set_objSysInfo_CreateObject_ADSystemInfo_gt_TEMP_ForestRootDomain_vbs_echo_Wscript_Echo_Forest_DNS_Name_amp_objSysInfo_ForestDNSName_gt_gt_TEMP_ForestRootDomain_vbs_if_i_not_2_">\{\} @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\}</a></h1>\{/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