Friday, March 14, 2008

Exchange 2007 Powershell Script - Emails owners of all email distribution groups

Last year I posted a generic script to enumerate all members of all email groups. My department was tasked with finding a way to keep all email groups updated for all departments. My solution has 2 parts:

Part 1 is configuring the "Managed By" field in Active directory or exchange for all distrubtion groups and checking the box for 'Manager can update member list'. This allows email distribution group owners to modify membership through their Outlook client directly. (via the Address book interface.)

Part 2 consists of the following Powershell script which finds all Email Distribution Groups in the forest and then sends an email for every email distribution group to that groups owner. The emails contain the primary SMTP address for reference and a list of all members of that group for quick viewing and confirmation.

# Enumerates all members of all Distribution Lists in Exchange 2007
# and all owners.
# Script will then proceed to email each owner a list of all
# members of each group.
# Uses cmdlets from exch2007
# 3/14/08
# By: Gnawgnu

#first get all distributionlists
$dl = get-distributiongroup

#then enumerate through them all and get all group members.
foreach ($group in $dl) {

#build group data
$groupName = "Group Name: " + $
$groupAddr = "Email Address: " + $group.PrimarySMTPAddress
write-host $groupName -foregroundcolor Green
$dlgm = get-distributionGroupMember $
$gOwner = get-user $group.ManagedBy.Name

#setup email - make sure to add to your whitelist for
#antispam if applicable.
$sender = "PickASMTPSenderEmailAddress"
write-host $sender
#get Email Address of group owner
$recipient = $gOwner.WindowsEmailAddress
write-host $recipient
$server = "YourSMTPServerGoesHere"
write-host $server
$subject = "Monthly Review required - Email Group: " + $group.Name.ToString()
write-host $subject
#Note: `r`n is a carriage return
$bText1 = "`r`nOwner:" + $group.ManagedBy.Name.ToString() + "`r`n"
$bText2 = $groupAddr.ToString() + "`r`n"
$bText3 = "group members: `r`n"
$bText4 = $dlgm | fl Name | out-String
$bText5 = "Please use your Outlook Client to make changes if needed.`r`n"
$bText6 = "If you are no longer the manager of this group, please notify IT.`r`n"

$body = $bText1 + $bText2 + $bText3 +$bText4 +$bText5
write-host $body.ToString()
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body

#send email
$client = new-object System.Net.Mail.SmtpClient $server
$client.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials



Albert Widjaja said...

Hi Man,

how to modify your powershell to display the membership of a certain user in a distribution group ?

from EMC, and double clicking the particular mailbox and go to "Member Of" tab I can display it but how to export that list anyway ?


Gnawgnu said...

So basically you want the script code to enumerate all email distribution groups that a particular user is a member of? I'll post the code snippet that you 'll need here:

Eric said...

Did you do step 1 with a script or manually?
Specifically I am looking to "checking the box for 'Manager can update member list'" via a script.


Gnawgnu said...

Sadly, manually via the active directory users mmc console.

Tim said...

I've been searching for a way to allow Distribution List Managers to modify list membership via a web-page. You instruct your users to modify membership via Outlook, which I agree would be the preferred method, however I have many users that don't have access to Outlook and still need to manage their respective DLs.

My understanding is that this can be accomplished via Exchange Web Services (EWS), however that is not quite my forte.

Any help pointers would be apprectiated.


Rena said...

Good post.

Jose said...

Can we achieve something similar on exchange 2003?

Gnawgnu said...

You might be able to do it but you'd have to use WMI calls and hope all the data is exposed that you need. Exch 2007 was the first version to have true powershell support. I'd start here

Jose said...

Since I'm new to scripting I was hoping to find a canned script that I use.