Exchange PowerShell: How to enumerate Distribution Lists, managers and members

Exchange PowerShell

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*

get-help distribAs 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.

get-distributiongroupSo 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

get-distributiongroup format-listNext we will use the Cmdlet Get-DistributionGroup managers | select Displayname, Primarysmtpaddress, Managedby

Get-DistributionGroup managers select Displayname Primarysmtpaddress ManagedbyNow 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

get-distributiongroup select displayname primarysmtpaddress -expand managedbyExcellent, 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

get-distributiongroup select displayname primarysmtpaddress -expand managedby select displayname primarysmtpaddress nameFinally 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.

get-distributiongroupmemberAs 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?

Get-DistributionGroup Get-DistributionGroupMembersLooking 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://social.technet.microsoft.com/Forums/windowsserver/en-US/3054cfe5-2886-40d1-913c-88059967726e/previous-object-in-a-pipe

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}}}

for-each get-distributiongroup get-distributiongroupmemberThis 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.

for-each get-distributiongroup write-host get-distributiongroupmemberSo, 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.

for-each get-distributiongroup get-distributiongroupmember ft expressionTo 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. How to install Exchange 2013 (SP1) on Windows Server 2012 R2

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

If you found this post useful, please share!

    Related Posts

    36 thoughts on “Exchange PowerShell: How to enumerate Distribution Lists, managers and members

    1. Regnar

      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

      Reply
    2. Michael

      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.

      Reply
      1. OxfordSBSguy.com

        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

        Reply
    3. Bruce Kosh

      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}

      Reply
      1. OxfordSBSguy.com

        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

        Reply
    4. Joshua Works

      Thank you for this script! Can you assist me with executing this script against distribution lists that have more than two members?

      Reply
    5. welly

      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

      Reply
    6. rmittler

      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!

      Reply
      1. Yenthe Rosiers

        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.

        Reply
    7. rmittler

      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?

      Reply
    8. Neil

      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}

      Reply
    9. BD

      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

      Reply
    10. Tom

      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 😉

      Reply
      1. Andy

        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.

        Reply
    11. A Good

      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}

      Reply
    12. Andy

      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

      Reply
    13. Terrence

      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

      Reply
    14. Peter Scobbie

      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

      Reply
    15. Sean Amick

      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

      Reply
    16. Hanna

      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?

      Reply
    17. jacob

      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.

      Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *