Q. How can I return the OU (Organizational Unit), or container, where my computer is located in Active Directory?

I have scripted CmpOUC.bat to return the Active Directory OU or container where my computer is located.

The syntax for using CmpOUC.bat is:

\[Call\] CmpOUC OUorContainer

Where OUorContainer is a call directed environment variable that will contain the computer's the OU or container.

CmpOUC.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: CmpOUC OUorContainer&goto :EOF
if exist "%TEMP%\CmpOUC.VBS" goto doit
@echo.sComputerDN = CreateObject("ADSystemInfo").ComputerName>"%TEMP%\CmpOUC.VBS"
@echo.if InStr(sComputerDN, ",CN=Computers,DC=") ^> 0 Then>>"%TEMP%\CmpOUC.VBS"
@echo. sOUDN="Computers,DC">>"%TEMP%\CmpOUC.VBS"
@echo. sOUDN = Mid(sComputerDN, InStr(sComputerDN, "OU=") + 3)>>"%TEMP%\CmpOUC.VBS"
@echo.END IF>>"%TEMP%\CmpOUC.VBS"
@echo.sComputerOU = Left(sOUDN, InStr(sOUDN, ",") - 1)>>"%TEMP%\CmpOUC.VBS"
@echo.Wscript.Echo sComputerOU>>"%TEMP%\CmpOUC.VBS"
for /f "Tokens=*" %%O in ('cscript //nologo "%TEMP%\CmpOUC.VBS"') do (
 set %1=%%O

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.