Avatar of LindyS
LindyS
Flag for United States of America asked on

Find Distribution Groups in Active Directory.

I have a strange request. I have been asked to find all the distribution groups in AD that have more than a specific number of users, say 20 for the sake of argument.
Is there a way to query AD and return a list of groups that only meet that criteria?
Any help would be appreciated.
Windows Server 2008PowershellActive Directory

Avatar of undefined
Last Comment
yo_bee

8/22/2022 - Mon
Pramod Ubhe

This can be done using powershell very easily -

Get-DistributionGroup | Get-DistributionGroupMember | Export-CSV C:\data.csv

above command will give you the DL list in a csv file and then you can apply filters as required. (It needs to be executed from an exchange shell)

or you may want to try this -

http://community.spiceworks.com/scripts/show/1388-export-distribution-list-members
Farhan Kazi

Sometime back I created following batch script for similar purpose, hope it works for you.
Save this script with any name having .cmd extention and execute with a number parameter.

Like: GroupWithMembers.cmd 10

@ECHO OFF
IF "%1" LEQ "0" ECHO Invalid parameter.&&GOTO :End
 
IF EXIST _Output.txt DEL _Output.txt
FOR /F "delims=" %%g IN ('DSQuery * -Filter "(&(ObjectClass=Group)(groupType=2))" -Limit 0') DO (
	ECHO Quering: %%g
	DSGET Group %%g -members >TmpMbrs.txt
	FOR /F %%c IN ('TYPE TmpMbrs.txt ^|FIND "=" /C') DO (
	IF %%c GTR %1 ECHO Group: %%g Members: %%c >>_Output.txt))

ECHO.&ECHO Script finished.
IF EXIST _Output.txt (
	START "" Notepad.exe _Output.txt) ELSE ( 
	ECHO No group with more than %1 members found.)
:End

Open in new window

LindyS

ASKER
This is close to what I need.
The only problem is that I have around 700 DLs, but this only queries 5 of them.
None of them are in the same OU.
How can I make sure it queries all OUs?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Farhan Kazi

Copy and paste following statement on command line and see if it is listing all your distribution groups?

DSQuery * -Filter "(&(ObjectClass=Group)(groupType=2))" -Limit 0

Open in new window

LindyS

ASKER
No, it still just lists the same 5 DLs.
Farhan Kazi

With this?
DSQuery * ForestRoot -Filter "(&(ObjectClass=Group)(groupType=2))" -Limit 0

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
yo_bee

Do you need the members or just list that meet the > 20 mark?
LindyS

ASKER
That gives me a bunch more, but from other domains in the forest.
It still only returns the same 5 from my domain.
I thought maybe it might be a problem on my workstation, but I get the same results from one of the DCs.
For the heck of it, I tried DomainRoot as well, but that only gave me the same 5 DLs as the first command.
LindyS

ASKER
yo_bee

All I need is the name of the group, and the count of members.
If not the count, just that it has more members than 20.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Farhan Kazi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
LindyS

ASKER
farhankazi

It looks like it is working; it was still running when I left the office.
I'll check it in the morning and get back to you.

Thanks
SOLUTION
yo_bee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
LindyS

ASKER
yo_bee
Yours gives a clean output, but it wouldn't work in Powershell. I changed the command to work with Quest management shell.

farhankazi
Your solution provides me with the information I need, I just need to work with the output to make it more readable.

Thanks to both of you for your help.
yo_bee

You need to run it from a computer that has ADDS Cmdlet installed
If you run it from your DC does it work?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.