I have scripted GetOUCN.bat to extract the container or OU from a distinguishedName.

The syntax for using GetOUCN.bat is:

call GetOUCN DN ID OUCN

Where:

<b>DN</b>   is the <b>distinguishedName</b>.

<b>ID</b>   is a <a href="/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain the letters <b>CN</b> or <b>OU</b>.

<b>OUCN</b> is a <a href="/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain the container or <b>OU</b> name.
GetOUCN.bat contains:
@echo off
if \{%3\}<h1><a name="_echo_Syntax_GetOUCN_DN_ID_OUCN_amp_goto_EOF_for_f_Tokens_2_Delims_a_in_echo_1_do_for_f_Tokens_1_Delims">\{\} @echo Syntax: GetOUCN DN ID OUCN&goto :EOF
for /f "Tokens=2 Delims=," %%a in ('@echo %1') do (
 for /f "Tokens=1* Delims</a></h1>" %%b in ('@echo %%a') do (
  set %2=%%b
  set %3=%%c
 )
)