Friday, August 31, 2012

How to export a list of Exchange 2010 Distribution Groups and membership details

I was asked today to make a document which lists all if the email addresses, distribution groups (DG), and DG memberships. If it was a small organization with a small mail server this wouldn't be a huge issue but when the number of DGs are well past a hundred than the task may seem daunting. I had no time to sit here and look through each and every single DG to see who the members are and enter it all in a spreadsheet so I decided use the Exchange Power Shell to see if I can get all of this information.

A quick search on the web reveals that this is indeed possible but methods from MS show how to get the membership information from a single DG and is not dynamic enough to read across an entire organization as it requires you to know the name of the DG and enter it each time. After some trial and error, I finally found out which portion of AD, ADSI, and Exchange values that the Exchange Power Shell can read and here is the script that will output a text file that includes User, User Name, and email address associated with a given DG;

write-output “” > C:\outputDGmembers.txt get-distributiongroup | Sort -Property DisplayName | foreach { $name = $_.displayname $output = ‘Group Name: ‘ + $Name write-output $output >> C:\outputDGmembers.txt Get-DistributionGroupMember $name | Sort -Property DisplayName | Select DisplayName, Alias, primarysmtpaddress >> C:\outputDGmembers.txt write-output “” “” >> C:\outputDGmembers.txt }

If you need more information in the output, you can add the appropriate values after the word 'Select'. As for getting the email addresses, an excellent script is available at Flaming Keys' site which simply works like a charm. Below is a re-post from Flaming Keys, please check out the site and read the whole article which explains each step.

# ------------------------------------------------------------------------------- # Script: Get-AllEmailAddresses.ps1 # Author: Chris Brown # Date: 25/07/2011 00:03 # Keywords: Exchange, Email, SMTP # comments: # # Versioning # 25/07/2011 CJB Initial Script # # ------------------------------------------------------------------------------- # Import the E2010 modules if available, otherwise import 2007's. if (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -Registered -ErrorAction SilentlyContinue) { # Found 2010, add it Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 } else { # Add 2007 Add-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.Admin } # Create an object to hold the results $addresses = @() # Get every mailbox in the Exchange Organisation $Mailboxes = Get-Mailbox -ResultSize Unlimited # Recurse through the mailboxes ForEach ($mbx in $Mailboxes) { # Recurse through every address assigned to the mailbox Foreach ($address in $mbx.EmailAddresses) { # If it starts with "SMTP:" then it's an email address. Record it if ($address.ToString().ToLower().StartsWith("smtp:")) { # This is an email address. Add it to the list $obj = "" | Select-Object Alias,EmailAddress $obj.Alias = $mbx.Alias $obj.EmailAddress = $address.ToString().SubString(5) $addresses += $obj } } } # Export the final object to a csv in the working directory $addresses | Export-Csv addresses.csv -NoTypeInformation # Open the csv with the default handler Invoke-Item addresses.csv

Now back to work on something else...

No comments:

Post a Comment