@ECHO OFF :: Use local variables IF "%OS%"=="Windows_NT" SETLOCAL :: Check Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Check number of command line arguments and validity of optional switch IF "%~1"=="" GOTO Syntax IF NOT "%~2"=="" IF /I NOT "%~2"=="/B" IF /I NOT "%~2"=="/C" IF /I NOT "%~2"=="/U" IF /I NOT "%~2"=="/V" GOTO Syntax :: Interpret the optional switch's value IF /I "%~2"=="/C" (SET ListU=0) ELSE (SET ListU=1) IF /I "%~2"=="/U" (SET ListC=0) ELSE (SET ListC=1) :: Run a query to check availability of DSQUERY and validity of the specified OU DSQUERY.EXE OU -name "%~1" -limit 2 2>NUL | FIND.EXE "=" >NUL IF ERRORLEVEL 1 GOTO Syntax :: The actual commands FOR /F "tokens=*" %%A IN ('DSQUERY.EXE OU -name "%~1"') DO ( IF /I "%~2"=="/V" ECHO.%%~A IF "%ListU%"=="1" DSQUERY.EXE User "%%~A" -name * -limit 10000 2>NUL | DSGET.EXE User -samid -display 2>NUL | FIND.EXE " " | FINDSTR.EXE /R /B /V /I /C:" samid " | SORT.EXE IF "%ListC%"=="1" DSQUERY.EXE Computer "%%~A" -name * -limit 10000 2>NUL | DSGET.EXE Computer -samid -desc 2>NUL | FIND.EXE " " | FINDSTR.EXE /R /B /V /I /C:" desc " | SORT.EXE ) :: Done ENDLOCAL GOTO:EOF :Syntax ECHO. ECHO OUMember.bat, Version 1.03 for Windows 2000 / XP / Server 2003 ECHO List all members, users and/or computers, of the specified OU(s). ECHO. :: Pipe symbols cannot be ECHOed in Windows 9x, so in that :: case we need to use an alternative character (ASCII 179) IF "%OS%"=="Windows_NT" ECHO Usage: OUMEMBER ou_name [ /B ^| /C ^| /U ^| /V ] IF NOT "%OS%"=="Windows_NT" ECHO Usage: OUMEMBER ou_name [ /B ³ /C ³ /U ³ /V ] ECHO. ECHO Where: "ou_name" is the name of the OU to be queried ECHO /B lists both users and computers (default) ECHO /C lists computers only ECHO /U lists users only ECHO /V like /B, and also lists DNs of matching OUs ECHO. ECHO Notes: This batch file uses DSGET and DSQUERY (native in Windows ECHO Server 2003, will work in Windows 2000 and XP if available). ECHO Increase DSQUERY's "-limit" value for networks where OUs ECHO may contain over 10000 members. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com :: Discard of local variables IF "%OS%"=="Windows_NT" ENDLOCAL