Enumerate Distribution Group Members
Note: Script updated on 2009/07/17 to fix two bugs. Read more here: http://www.tinyint.com/index.php/2009/07/17/enumerate-distribution-groups-script-updated/
In my organization, we make use of many different groups to separate departments and sub-groups of each department, and many groups build off of this. We also make use of Dynamic Distribution Groups to make things a bit easier on the admin side of things. When tasked with cleaning up these distribution groups and making them easier to manage, it was a bit difficult determining who was supposed to receive mail for what group.
This is because the Get-DistributionGroupMember cmdlet doesn’t have a parameter like –expand which will give you all of the child groups and their members as well. If you have a group called “Engineering” which then has 4 child groups for each department and then each of those groups has each individual mailbox, when you perform “Get-DistributionGroupMember -Identity Engineering”, you will only see the four child groups, not each member of those as well. This became a big issue because of how much we rely on sub-groups in our organization, and after a lot of investigation it turned out there was no way to do this built in directly to any cmdlet, so I wrote a script that would do this for me. If you need to recursively enumerate distribution group members you are unfortunately out of luck with built in cmdlets.
There are a number of scripts out there that serve a similar purpose as the one I have created, but most do not handle mail contacts or dynamic distribution groups, so I figured if I am going to have to add functionality, I might as well write it myself from the ground up. So now, if you are ever in need of getting child members of a distribution group you can use this script to help you out.
One nice feature here is if you specify “-showTree” as a parameter, it will display a treeview of all the groups. Without -showtree it will just grab all child members and display them in a flat view. The script is pretty long because I included help text that displays if you run the script without any parameters, and for that reason I am just posting it as a download link. Hope this helps a few people out there who went through the same trouble I did finding out that there is no built in way to do this!
Download Link:
Download enumerate_groups.ps1

This is a great script – but I am having trouble getting it piped into a CSV or TXT file. Are you able to help me out with this please?
Thanks for the comment! Sadly, piping out to a file is a bit annoying because of the way the output gets formatted. The best way to do this is by using start-transcript before you run the script, which makes sure you get the proper output. Unfortunately, CSV is not something that will work with this however because the data is not presented in a way that would be usable by the CSV formatter.
I have wanted to correct both of these problems in an update of the script but I simply haven’t had the time to revisit this in a while. Hope start-transcript helps you get what you are looking for!
It would be nice if it output an array of mailbox objects. I am not a coder so I could not determine from the script if that would even be possible. I want to use the script to then manipulate a list of mailboxes.
This is definitely possible, I just have not had time to go back and rewrite the script to do that… maybe I’ll actually do my rewrite later this week…
Out of curiosity, what are you trying to do that you need the array?