Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find Distribution Groups in Active Directory.

Posted on 2013-01-01
14
Medium Priority
?
848 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:Farhan Kazi
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 26

Expert Comment

by:Farhan Kazi
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:Farhan Kazi
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:
Farhan Kazi earned 1200 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 800 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

610 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