Solved

Find Distribution Groups in Active Directory.

Posted on 2013-01-01
14
844 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 23

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 23

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 23

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

729 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