Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

list users in groups in OU

Hey Experts.

I would like to show the users in the various groups within a particular OU.

Department (OU)
>Computers (OU)
>Groups (OU)
>>PRT-1 (Group)
>>PRT-2 (Group)
>>PRT-3 (Group)

I want to have the users that are in the groups nested in the "Department OU > Groups OU" output to a log file.  I need to know the users listed in the particular group:

PRT-1
-userA
-userB

PRT-2
-userA
-userC
-userD

Thanks experts!!
Avatar of NVIT
NVIT
Flag of United States of America image

Note: Change YourDomainName to your actual domain name.

From the DC, try:
dsquery user "CN=PRT-1,OU=Groups,OU=Computers,DC=YourDomainName,DC=com"

If that works, try this at CMD prompt:
for /f "tokens=1-2 delims==," %a in ('dsquery user "CN=PRT-1,OU=Groups,OU=Computers,DC=YourDomainName,DC=com"') do (echo %~b)
Actually, change that to dsget group "CN=GroupName,DC=domain,DC=name,DC=com" -members
This should get you where you want to be.

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=Domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.txt
::pause
for /f "tokens=1-2 delims==," %%a in ('dsget group %ougroup% -members ^|sort') do (echo %%~b >>ou-group-members.txt)
goto :eof

Open in new window

This will be a little nicer output file.

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=Domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.csv
::pause
for /f "tokens=1-2 delims==," %%a in ('dsget group %ougroup% -members ^|sort') do (echo ,%%~b >>ou-group-members.csv)
goto :eof

Open in new window

Avatar of samiam41

ASKER

Great script!

Here is what I used:

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=Domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.csv
::pause
for /f "tokens=1-2 delims==," %%a in ('dsget group %ougroup% -members ^|sort') do (echo ,%%~b >>ou-group-members.csv)
goto :eof

Open in new window


The log file shows the username as:  lastname\

Not the user name.  How can I (we/you) fix that?
ASKER CERTIFIED SOLUTION
Avatar of Gabriel Clifton
Gabriel Clifton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant work!!  Thanks for your help with this!
No problem, any time.
Check this question out if you have some time and want some points!

https://www.experts-exchange.com/questions/28602773/insert-code-to-prompt-for-OU-to-audit.html
Hey everyone!  I have a question that received a couple of replies but the suggestions are for powershell and I asked for batch.  If any of you have a minute or two and would like some points, I would appreciate your help.

https://www.experts-exchange.com/questions/28610287/Add-members-in-one-group-to-another-group-via-script.html