Solved

Find Distribution Groups in Active Directory.

Posted on 2013-01-01
14
821 Views
Last Modified: 2013-01-02
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.
0
Comment
Question by:LindyS
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 10

Expert Comment

by:Pramod Ubhe
ID: 38734365
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
0
 
LVL 26

Expert Comment

by:farhankazi
ID: 38734414
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

0
 
LVL 3

Author Comment

by:LindyS
ID: 38734428
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?
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 26

Expert Comment

by:farhankazi
ID: 38734457
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

0
 
LVL 3

Author Comment

by:LindyS
ID: 38734469
No, it still just lists the same 5 DLs.
0
 
LVL 26

Expert Comment

by:farhankazi
ID: 38734484
With this?
DSQuery * ForestRoot -Filter "(&(ObjectClass=Group)(groupType=2))" -Limit 0

Open in new window

0
 
LVL 22

Expert Comment

by:yo_bee
ID: 38734519
Do you need the members or just list that meet the > 20 mark?
0
 
LVL 3

Author Comment

by:LindyS
ID: 38734540
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.
0
 
LVL 3

Author Comment

by:LindyS
ID: 38734546
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.
0
 
LVL 26

Accepted Solution

by:
farhankazi earned 300 total points
ID: 38734561
Sorry my mistake - In above statement I only mentioned GroupType=2 which means Global Distribution groups (not Domain Local & Universal).

Check following:

@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)(groupType=4)(groupType=8)))" -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

0
 
LVL 3

Author Comment

by:LindyS
ID: 38734652
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
0
 
LVL 22

Assisted Solution

by:yo_bee
yo_bee earned 200 total points
ID: 38734803
I see that you may have a solution, but I figured I lend my little snippet.
Copy and paste into notepad and save as a .ps1
from Run or Command prompt.
powershell script.ps1 > output.txt

import-module activedirectory

$Grouplist = Get-ADGROUP -Filter 'GroupCategory -eq "Distribution"'  

forEach ($group in $grouplist)
{

 $a = (Get-ADGroupMember -identity $group.name | Measure-Object)
 
 if ($a.count -ge '20')
 {
 write-host $group.name   $a.count
 }
  
}

Open in new window

0
 
LVL 3

Author Closing Comment

by:LindyS
ID: 38735651
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.
0
 
LVL 22

Expert Comment

by:yo_bee
ID: 38736434
You need to run it from a computer that has ADDS Cmdlet installed
If you run it from your DC does it work?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question