A common request is to get a list of all distribution groups and the members contained in that distribution group. The question came up on a mailing list I frequent today, and my initial response was a PowerShell “one-liner” (actually five lines, but all a single PowerShell statement).
The one-liner worked, but it had a couple of limitations: it wasn’t very pretty (that is, the output was formatted poorly) and if there were than one user set to manage the distribution group, that would be reportedly incorrectly. It was also slower than it had to be.
Note: a distribution group may also be a security group. From an Exchange Server perspective, the important thing is whether the group is mail-enabled or not.
So, I took that five-liner, cleaned it up, fixed the ManagedBy reporting bug, and sped it up (by using an embedded pipeline to report on the members contained in a distribution group). That turned it into a 50+ line script (which includes 10 lines of comments!). As I said on the mailing list – you can generate quick results with PowerShell. That’s good enough for most admins. But if you want it pretty and “production quality” then it’s going to take a little more time.
You can take this report and pipe it to out-string in order to save the output to a disk file. Then you can inspect the file later, email it, copy-n-paste it, whatever you want.
Here is the script:
## ## Report-DistributionGroupsAndMembers.ps1 ## v1.1 ## ## Michael B. Smith ## http://TheEssentialExchange.com ## August, 2010 ## ## Requires Exchange Management Shell ## Should work with either PowerShell v1 or v2 ## Tested on both Exchange 2007 and Exchange 2010 ## function formatManager($formatstring, $manager) { $formatstring -f $manager.Name, ($manager.Parent.ToString() + "/" + $manager.RDN.ToString().SubString(3)) } Get-DistributionGroup -ResultSize Unlimited |% { $group = $_ "Group Name & Identity: {0}, {1}" -f $group.Name, $group.Identity $managedBy = $group.ManagedBy if( $managedBy -is [Microsoft.Exchange.Data.Directory.ADObjectId] ) { formatManager "Group manager: {0}, {1}" $managedBy } elseif( $managedBy.Count -gt 1 ) { [bool]$first = $true foreach( $manager in $managedBy ) { if( $first ) { formatManager "Group managers: {0}, {1}" $manager $first = $false } else { formatManager " {0}, {1}" $manager } } } elseif( $managedBy.Count -gt 0 ) { formatManager "Group manager: {0}, {1}" $managedBy[0] } "Members:" Get-DistributionGroupMember -Identity $group.Identity -ResultSize Unlimited |% { foreach( $member in $_ ) { "`t$($member.Name)" } } "---" }
Until next time…
If there are things you would like to see written about, please let me know.
–Edit on August 20, 2010
The original script would not display ManagedBy on Exchange 2007 (I’m running Exchange 2010). This is because ManagedBy in Exchange 2010 always returns an array-type object (it can contain multiple users). In Exchange 2007, the ManagedBy value is always a singleton. The required changes to the script are in red.
Michael B.
Follow me on twitter: @EssentialExch