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:

                              DN   is the distinguishedName.                              ID   is a call directed environment variable that will contain the letters CN or OU.                              OUCN is a call directed environment variable that will contain the container or OU name.
GetOUCN.bat contains:
                              @echo off                              if \{%3\}

\{\} @echo Syntax: GetOUCN DN ID OUCN&goto :EOF for /f "Tokens=2 Delims=," %%a in ('@echo %1') do ( for /f "Tokens=1* Delims

" %%b in ('@echo %%a') do ( set %2=%%b set %3=%%c ) )