Powershell command to list all distribution groups and members of those groups.
I have been tasked with listing all of the distribution groups in our org and showing all members of every group. I have tired using get-distributiongroup | get-distributiongroupmember But it only show the members and not the groups. Any scripters out there got a quick one liner I could use? I really don't want to have to go through all 200 distribution groups manually exporting members to a .csv for each group. Thanks in advance!
November 2nd, 2009 4:59pm

If you use Quest Free PowerShell Commands for Active Directory you can do a:Get-QadGroup -GroupType "Distribution"| Get-QadGroupMember -Indirecthttp://www.quest.com/powershell/activeroles-server.aspxIn EMS it's harder, but I can post a script if you'd like,Karl
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2009 6:09pm

I have seen the Quest Free PowerShell Commands but we are not using them. If you wouldn't mind I would really appreciate it if you could post your script. I would like to see how this works natively in EMS. Thank you
November 2nd, 2009 8:49pm

Hi,Does this help....$DGrps = Get-DistributionGroupForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrpforeach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }}Nitin Gupta (gupnit) | MVP - Exchange | http://www.nitingupta.in/blogs
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2009 9:23pm

This does appear to work but how do I go about outputting the file to somewhere? I entered C:\data.csv for <yourfilename> but never see the file created and when I create a blankfile called C:\data.csv hoping that the data will append to the file it remains empty.Thank for your help!
November 3rd, 2009 10:12am

Thanks for posting your script Karl. In less I am doing something wrong the script still asks that I submit the anme of the group I want to know about. I need to know this information for all DL Groups at one time. Is there away to tell the script to give me information about all of my groups?Thank you
Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2009 1:36pm

Try this:Get-DistributionGroup -ResultSize unlimited |%{.\get-exgrouprecurse $_.Name}I am pretty sure this will not show the name of the main group - like:Group Ahas a member member 1and a member Sub Group 1Which has a memberMember 2I could modify it at some point, or perhaps you can follow it and modify it.Karl
November 3rd, 2009 3:56pm

At a quick glance, Nitin's script should do what you're looking for with a few modifications. Write-host is what's preventing the output from going to the file like you're trying to accomplish. You can modify the script to use the re-directors, or just use out-file. I haven't tested it, but this should work (just fixing a few bits from Nitin's script, but other then that, really just a copy and paste of his post.) $DGrps = Get-DistributionGroup ForEach ($DGrp in $DGrps) {out-file -inputobject $DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp foreach ($GrpMbr in $GrpMbrs) {out-file -inputobject $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }} Good luck, Dan Holton
Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2009 7:25pm

Thanks Mate....I have not tested it, I should have mentioned that, thought it should do the trick.Nitin Gupta (gupnit) | MVP - Exchange | http://www.nitingupta.in/blogs
November 3rd, 2009 9:56pm

Jon-alfred:I was looking for a way on how to do this, and your script was exactly what i was looking for. Thanks!
Free Windows Admin Tool Kit Click here and download it now
November 17th, 2009 2:54pm

Can someone help provide some information on how to modify this script? I would like the output to show what groups the users belong to. So for Joe Smith, I want to see that he is part of Accounting, NY Office, FAS etc.
May 6th, 2010 4:32pm

Hi, Does this help.... $DGrps = Get-DistributionGroup ForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp foreach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }} Nitin Gupta (gupnit) | MVP - Exchange | http://www.nitingupta.in/blogs Hi Nitin Gupta, thanks for the script. Executing just the code: $DGrps = Get-DistributionGroup ForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp foreach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }} I get an output like this: DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group1 -Filepath c:\Temp\1.txt -append DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group2 -Filepath c:\Temp\1.txt -append DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group3 -Filepath c:\Temp\1.txt -append DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group4 -Filepath c:\Temp\1.txt -append DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group5 -Filepath c:\Temp\1.txt -append DGrp.name -Filepath c:\Temp\1.txt -append = Get-DistributionGroupMember -id Group6 -Filepath c:\Temp\1.txt -append What is wrong? Thanks a lot!Piero Bacarella - IT Professional - Rome(Italy) - http://www.it-resources.info
Free Windows Admin Tool Kit Click here and download it now
January 7th, 2011 10:30am

On Fri, 7 Jan 2011 15:30:00 +0000, Piero Bacarella wrote: >Hi, Does this help.... $DGrps = Get-DistributionGroup ForEach ($DGrp in $DGrps) {Write-Host DGrp.name -Filepath <yourfilename> -append $GrpMbrs = Get-DistributionGroupMember -id $DGrp foreach ($GrpMbr in $GrpMbrs) {Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append }} Don't you just love the way HTML munges things? :-( Besides the formatting, there was a missing "$" in the 1st "ForEach". I added semicolons at the end of each statement so they don't all run together if you just copy the text. $DGrps = Get-DistributionGroup; ForEach ($DGrp in $DGrps) { Write-Host $DGrp.name -Filepath <yourfilename> -append; $GrpMbrs = Get-DistributionGroupMember -id $DGrp; foreach ($GrpMbr in $GrpMbrs) { Write-Host $GrpMbr.DistinguishedName -Filepath <yourfilename> -append } } --- Rich Matheisen MCSE+I, Exchange MVP --- Rich Matheisen MCSE+I, Exchange MVP
January 7th, 2011 3:21pm

Hello. With write-host won't work for me, so had to change your script, like that. $DGrps = Get-DistributionGroup; ForEach ($DGrp in $DGrps) { $DGrp.name |Out-file -Filepath <Filename>-Append; $GrpMbrs = Get-DistributionGroupMember -id $DGrp; foreach ($GrpMbr in $GrpMbrs) { $GrpMbr.DistinguishedName |Out-file -Filepath <Filename> -Append; } }
Free Windows Admin Tool Kit Click here and download it now
March 14th, 2011 10:55am

I know its a couple years later, but I saw this and here is what I use, very quick and simple. Get-DistributionGroupMember -identity "group name" | ft "PrimarySMTPAddress" >c:\export-list.txt -BV
May 9th, 2011 10:51pm

Hi All I tested that script of O. Brazhko and Note: We must save a filename have there's script, eg: c:\AllDistributionGroup.ps1 Body of script: $DGrps = Get-DistributionGroup; ForEach ($DGrp in $DGrps) { $DGrp.name |Out-file -Filepath c:\DG.csv -Append; $GrpMbrs = Get-DistributionGroupMember -id $DGrp; foreach ($GrpMbr in $GrpMbrs) { $GrpMbr.DistinguishedName |Out-file -Filepath c:\DG.csv -append; } } And then we open short Exchange PowerShell run as Administrator (not check restriction permittion ). At point [PS] ... >we can input path and file name script . eg: c:\AllDistributionGroup.ps1 wait a minutes... We can look file result in c:\DG.csv Good luck ! Thang.Le
Free Windows Admin Tool Kit Click here and download it now
June 17th, 2011 5:08am

I changed your script. Group Name and other information are same row in a file. Thank you. $DGrps = Get-DistributionGroup; ForEach ($DGrp in $DGrps) { $GrpMbrs = Get-DistributionGroupMember -id $DGrp; foreach ($GrpMbr in $GrpMbrs) { $DGrp.name+","+$GrpMbr.DisplayName+","+$GrpMbr.Title+","+$GrpMbr.Alias+","+$GrpMbr.WhenMailboxCreated+","+$GrpMbr.DatabaseName+","+$GrpMbr.HiddenFromAddressListsEnabled+","+$GrpMbr.PrimarySmtpAddress+","+$GrpMbr.DistinguishedName+","+$GrpMbr.Identity+","+$GrpMbr.WhenChanged+","+$GrpMbr.WhenCreated |Out-file -Filepath c:\DG1.csv -append; } }
March 7th, 2012 10:54am

Hi, How to modify the script so that it can read and retrieve all Email distribution group from the exchange server ? rather than typing it one by one ?/* Server Support Specialist */
Free Windows Admin Tool Kit Click here and download it now
March 13th, 2012 6:50pm

Hi, How to modify the script so that it can read and retrieve all Email distribution group from the exchange server ? rather than typing it one by one ? /* Server Support Specialist */ Hi, Most of the scripts mentioned above retrieve all DGs like this: $DGrps = Get-DistributionGroup; ForEach ($DGrp in $DGrps) { } When you don't mention any name or identity after Get-DistributionGroup, then it returns all of the DGs, and if you have more than 1000 DGs in your environment, then you have to put -ResultSize Unlimited like this: $DGrps = Get-DistributionGroup -ResultSize Unlimited; ForEach ($DGrp in $DGrps) { } Regards,Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
March 14th, 2012 10:25am

thanks for the response, by the way I'm trying to modify the following script from http://www.tinyint.com/index.php/2009/05/24/enumerate-distribution-group-members/ so that it accept the looping as per your suggestion but it failed. $DGrps = Get-DistributionGroup -ResultSize Unlimited ForEach ($DGrp in $DGrps) { .\enumerate_groups.ps1 $DGrp -showTree } can you please assist me here ? Thanks once again. /* Server Support Specialist */
Free Windows Admin Tool Kit Click here and download it now
March 14th, 2012 11:34pm

thanks for the response, by the way I'm trying to modify the following script from http://www.tinyint.com/index.php/2009/05/24/enumerate-distribution-group-members/ so that it accept the looping as per your suggestion but it failed. $DGrps = Get-DistributionGroup -ResultSize Unlimited ForEach ($DGrp in $DGrps) { .\enumerate_groups.ps1 $DGrp -showTree } can you please assist me here ? Thanks once again. /* Server Support Specialist */ Hi, Please try replacing this line \enumerate_groups.ps1 $DGrp -showTree with \enumerate_groups.ps1 $DGrp.Identity -showTree or with \enumerate_groups.ps1 $DGrp.DistinguishedName -showTree Regards,Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
March 17th, 2012 4:14pm

I just used this on a Windows 2008 R2 server running Exchange 2010. I noticed that I was getting a bunch of pipe errors stating that the next command couldn't be run because a pipe command was already in progress. I added "Start-Sleep -ms 500" statements here: # Loop through all distribution groups ForEach-Object { Start-Sleep -m 500 And here: # Loop through the DG and assign each member name to the variable $member for ($i = 0; $i -lt $mem.Count; $i++) { Start-Sleep -m 500 $member = $mem[$i].name and that seemed to fix it! Thanks Jon, this saved me a bunch of time!
Free Windows Admin Tool Kit Click here and download it now
May 17th, 2012 3:38pm

Worked for me! Thanks!
June 18th, 2012 8:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics