Continuing with my series on Exchange PowerShell in this post we will look at how to enumerate distribuition lists, their managers and list their members.
From time to time I’m asked to check or update distribution lists for clients. The PowerShell Cmdlets we are going to be looking at today will allow us to generate a CSV file of distribution lists, their managers and a list of members for each list. It can be used as part of a basic audit of an Exchange System.
Distribution Group Managers
Firstly we have to find out which PowerShell Cmdlet to use. So in an Exchange Management Shell lets start with the Cmdlet:
Get-Help *distrib*
As you can see there are a few Cmdlets with ‘distrib’ in the name, but Get-DistributionGroup and Get-DistributionGroupMember looks promising.
Next lets run the Cmdlet Get-DistributionGroup to see what information it produces.
So it already gives us some useful information. Let’s take a look at the Managers Distribution Group to see what other information we can obtain.
Get-DistributionGroup Managers | Format-List
Next we will use the Cmdlet Get-DistributionGroup managers | select Displayname, Primarysmtpaddress, Managedby
Now we are going to combine the Select Cmdlet with the ExpandProperty Cmdlet to see if there is any other information associated with the ManagedBy property.
Get-DistributionGroup managers | select Displayname, Primarysmtpaddress -ExpandProperty Managedby
Excellent, as you can see the Name property is far more readable.
So our command will look like this to list the managers of all distribution groups.
Get-DistributionGroup | select Displayname, Primarysmtpaddress -ExpandProperty Managedby | Select Displayname, Primarysmtpddress, name
Finally to export it to CSV we add the Export-CSV Cmdlet. So our final command looks like this:
Get-DistributionGroup | select Displayname, Primarysmtpaddress -ExpandProperty Managedby | Select Displayname, Primarysmtpddress, name | Export-CSV c:\temp\DistributionGroupManagers.csv
Distribution Group Members
Now we know who manages the Distribution Groups, lets enumerate the members.
First lets start off with the Cmdlet Get-DistributionGroupMember.
As you can see it prompts you for an identity, in this case I used my Managers Distribution Group.
So what happens if we pipe Get-DistributionGroup into Get-DistributionGroupMembers?
Looking through the list i can see a number of duplicate names, so it looks like it is listing the members of each distribution group, but without the distribution group name it is pretty useless.
I’m still pretty new to PowerShell and spent ages playing with the Cmdlets and trying to figure out a way of listing the Distribution Group name in the output to no avail. In my research though I found the two links below which I used as the basis for the rest of this post.
http://technet.microsoft.com/en-us/library/ee692794.aspx
From the first link above i used the Cmdlet below provided by Christian Schulenburg and produced the following output.
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft alias, @{label=’Test1′;expression={$group.name}}}
This is looking much more promising now.
I modified the script a little and used the Write-Host Cmdlet to output the $group vairable as a header, but it didn’t make it as readable as i had hoped for.
So, using the second Technet link above, I went about creating a custom table to display the results. I ended up with the command below
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft @{expression={$_.displayname};Label=”$group”}}
Now this gives us the following output. Which looks pretty readable to me, and is exactly what I am after.
To output the data to a file I used the Out-File Cmdlet along with -append. So the final Cmdlet looks like this:
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:\temp\DistributionListMembers.txt -append}
The PowerShell command above works on both Exchange 2007 and Exchange 2010.
Edit 20/11/2014
To sort alphabetically within each group add the sort displayname into the command as below:
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:\temp\DistributionListMembers.txt -append}
Related Posts:
1. Office 365 / Exchange: Stop Display Name Spoofing
2. Exchange 2013 Initial Configuration Settings multi-part series
3. Exchange PowerShell: How to find users hidden from the Global Address List
4. Exchange PowerShell: How to list all SMTP email addresses in Exchange
5. Office 365 PowerShell: How to bulk change Office 365 calendar permissions using Windows PowerShell
Thanks – needed to work with Exchange Distribution groups in Excel – ended up with
$( foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | select-object @{Name=’Group’;Expression={$group.Name}}, @{Name=’GroupSMTP’;Expression={$group.primarysmtpaddress}}, Name, PrimarySmtpAddress } ) | export-csv DistributionGroupMembers.csv -notype
Hi Regnar, thanks I’ll give it a try!
Cheers,
Carl
Thanks – this gets me very close to what I need! Is there a way to add alternate email addresses in additon to the primary? Sorry to ask for so much but I also need the delivery management ( allow only insde or inside and outside) as well as ownership for the group if possible.
Any suggestions on how to generate a list of Contacts that are not a member of any groups? I’ve been trying for a while to do this, but I can’t figure it out.
Hi Michael,
You could try something like: get-recipient | where {$_.addresslistmembership -eq “!Null”} or you could try get-recipient | where {$_.addresslistmembership -ne “Distribution Group” or “distributiongroup2”}
Let me know how you get on.
Cheers,
Carl
Hi OxfordSBSguy. On the last powershell command, how can you sort the members inside the output. So the members will show up alphabetically. Thanks a lot
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:tempDistributionListMembers.txt -append}
Just add a sort displayname into the command so it looks like this:
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:tempDistributionListMembers.txt -append}
Cheers,
Carl
Thank you for this script! Can you assist me with executing this script against distribution lists that have more than two members?
Great stuff thanks
This was helpful – thank you!
This was brilliant and exactly what i needed to finish a script i was working on. Thank you!
HI Carl,
how re you? i have a question about Ms. Exchange 2013. How to generate all distribution group list filter by delivery management ( Only sender inside my Organization or Senders inside and outside of my organization )
if possible i want to export the list into CSV format .
need your advice and help..
Thanks,
Welly
That’s not something I’ve done before, but with the project I’m working on at the moment it may well be in the next few weeks. If you find out before me let me know!
Hi Carl! Cool script! How would I add in the output file the email addresses along with the display name? I tried it out myself and it did not work ….
Thanks!
Do you mean for the distribution list or individual users?
foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label=”$group”}, @{expression={$_.PrimarySmtpAddress};Label=”Emailadres”} | Out-File c:tempDistributionListMembers.txt -append}
That should do it.
Can i get a list of all groups shows the status of the parameter “Hide group from Exchange adress list”
I mean the same list you have you but also adding to that list the users email address along with the rest of the info. So you have broken down into groups and then you have the user name. How would I add in the email address of the user next to the name as well?
Very helpful, many thanks. It might also be helpful if you have several distribution groups to sort them by the group name as well as by the username which you can do by adding another sort command:
foreach ($group in Get-DistributionGroup | sort displayname) { get-distributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:tempDistributionListMembers.txt -append}
Is it possible to ad in the user OU?
Hi Carl, thank you for taking the time to provide this script, when using this script , or others like it, to do an audit, the problem I experience is the “unlimited” factor which actually does have a limit. So what I would like to do is, without touching Exchange, run this script but when it hits the 10000 limit and stops, it starts again but this time it picks up where it left off and appending to a CSV file or similar, the next 10000.
The solution for this would be great for any powershell audit, where the results are incomplete due to the Exchange having a built-in limiting factor that ignore “unlimited”.
Thank you for your time
Regards
BD
expandproperty managedby and a empty managedby attribut exlude these distributiongroups without any erros. Dangerous when you trust these output and send this unchecked to your managers 😉
Thanks for the heads up Tom.
Get-DistributionGroup -resultsize unlimited | Select-object Name,@{label=”ManagedBy”;expression={[string]($_.managedby | foreach {$_.tostring().split(“/”)[-1]})}},Primarysmtpaddress | Export-Csv c:\temp\disgroups.csv
Gets you all Distribution Lists with or without a Managed By value, and includes the Managed By value where present.
Thanks Andy, I’ll give it a try.
Cheers,
Carl
http://www.OxfordSBSGuy.com
Thanks a ton for this script. I’m attempting to output the Recipient type details and smtp of the *groups* not the users within the groups. I’ve tried to tweak this command but cannot get it to output correctly into the groups provided. Any help would be greatly appreciated. Here is the command I’ve been using:
foreach ($group in Get-DistributionGroup | sort displayname) { get-distributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label=”$group”}, @{expression={$_.PrimarySmtpAddress};Label=”Email Address”}, @{expression={$_.notes};Label=”Notes”}, @{expression={$_.RecipientTypeDetails};Label=”Group Type”} | Out-File \\desktop\DistributionListMembers2.txt -append}
If you get an error about argument transformation, just change “get-distributiongroupmember $group ”
to ” get-distributiongroupmember $group.Identity”
Very helpful, thanks!
I’m doing a cross-forest move and wanted to make sure my groups were consistent.
Just thought I’d add to the conversation that the command you arrived at above lists only Distribution Groups where a Managed By value has been set. If Managed By is blank, the Distribution Group isn’t listed in the exported CSV.
To get a list of all Distribution Groups including the Managed By value(s) if one is set, I used the following –
Get-DistributionGroup -resultsize unlimited | Select-object Name,@{label=”ManagedBy”;expression={[string]($_.managedby | foreach {$_.tostring().split(“/”)[-1]})}},Primarysmtpaddress | Export-Csv c:\temp\disgroups.csv
Merci pour cet article
Thanks to all that posted before me! I also needed this information but I also wanted to know which groups had external contacts. I ended up with the solution below and then just filtered the results in the csv based on recipient type. Hope this helps someone else out!
$( foreach ($group in Get-DistributionGroup -resultsize unlimited) { get-distributiongroupmember $group | select-object @{Name=’Group’;Expression={$group.Name}}, @{Name=’GroupSMTP’;Expression={$group.primarysmtpaddress}}, Name, PrimarySmtpAddress,RecipientType } ) | export-csv ./DistributionGroupMembers3.csv -notypeinformation
Visited many other search results on the subject but this site has been the most helpful / useful and relevant – saved me a lot of effort to get at this data. Many thanks to all who contributed
Just a note on ResultSize…you need that parameter for both queries:
$( foreach ($group in Get-DistributionGroup -resultsize unlimited) { get-distributiongroupmember -resultsize unlimited $group | select-object @{Name=’Group’;Expression={$group.Name}}, @{Name=’GroupSMTP’;Expression={$group.primarysmtpaddress}}, Name, PrimarySmtpAddress,RecipientType } ) | export-csv ./DistributionGroupMembers3.csv -notypeinformation
I tried running all versions listed above, but not one of them list empty groups. Is it possible to list ALL distribution groups, including the empty ones?
AWESOME SCRIPT! Thank you for posting it!
Hi Everyone,
We have a Server where we host emails for our Customers, We have several OU’s created for different clients and for each client i need to know Distribution groups with its members.
I can be able to list or view members of a particular Group but not all. Please help me with a command where i can view all Distribution groups in that OU.
As i already mentioned that several OU’s created for our clients. so i need all distrubution groups in that OU.
Thanks for knowledge sharing. it really useful.
Was looking a way to ‘backup’ DL data before migrating them, into a single CSV. This finally did it:
$dl = Get-DistributionGroup
$dl | foreach {$DLaddress = $_.PrimarySmtpAddress; Get-DistributionGroupMember -Identity $_.name | select name,@{n=’DLPrimarySMTPAddress’;e={$DLaddress}}} | export-csv C:\users\supportmig\desktop\tests.csv -notype
Nice work,
I’m trying to export CSV with header and details all in one row.
Example:
Name : type : primarysmtp : members
——————————————————————
distroname : distribution : distroname@domain.com : user1,user2,user3
Even better if more details – MemberJoinRestriction, DeliveryRestrictions (Show members allowed to send)