Solved

Find Distribution Groups in Active Directory.

Posted on 2013-01-01
14
830 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
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 …

828 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