Solved

Find Distribution Groups in Active Directory.

Posted on 2013-01-01
14
804 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 26

Expert Comment

by:farhankazi
Comment Utility
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
Comment Utility
No, it still just lists the same 5 DLs.
0
 
LVL 26

Expert Comment

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

Open in new window

0
 
LVL 21

Expert Comment

by:yo_bee
Comment Utility
Do you need the members or just list that meet the > 20 mark?
0
How does your email signature look on mobiles?

Do your employees use mobile devices to reply to emails? With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices.

 
LVL 3

Author Comment

by:LindyS
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 21

Assisted Solution

by:yo_bee
yo_bee earned 200 total points
Comment Utility
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
Comment Utility
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 21

Expert Comment

by:yo_bee
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now