Step-By-Step: Adding Groups To AcceptMessagesOnlyFromDLMembers Exchange Attribute via PowerShell

The AcceptMessagesOnlyFromDLMembers attribute in Exchange allows you to configure the mail recipient to accept messages from members of specific distribution lists. The only issue is that there is no built in method for appending a distribution list (DL) to an existing list of DLs. To add to this conundrum, setting the AcceptMessagesOnlyFromDLMembers to equal a value overwrites what was there previously. I wrote a quick script to remedy this by appending a value instead of overwriting it. The remote Exchange Management Shell and the AD management module are required to enable this.  Here is the script created:

 function Add-AcceptMessagesOnlyFromDLMembers
     param (

     $arr = $(Get-MailContact $AppendTo | Select-Object
     $arr += ($(Get-ADGroup $NameOfGroup -Properties CanonicalName).CanonicalName)
     set-mailContact $AppendTo -AcceptMessagesOnlyFromDLMembers:"$($arr)"

Line 1 declares the function named Add-AcceptMessagesOnlyFromDLMembers. It is more verbose than I’d usually like to make it, however I am a fan of descriptive function and cmdlet names.

Lines 2-9 applies the parameters of the mail recipient whose AcceptMessagesOnlyFromDLMembers value we’re appending something to, and the DL that we’re appending.

Line 11 is where the magic happens. Acquiring the mail contact and select just the value currently in AcceptMessagesOnlyFromDLMembers so I can append something to it. I store that data in  $arr.

On line 12, the CanonicalName attribute is being retrieved for the DL I want to append to the list of DLs that can send mail to this contact. The AcceptMessagesOnlyFromDLMembers attribute is a bit finicky in that it only appears to take Canonical Names, not Distinguished names, etc.. I’m appending that value to the end of  $arr.

Line 13 is pretty straight forward as it sets the AcceptMessagesOnlyFromDLMembers attribute to the value of  $arr determined in line 12.

Please feel free to utilize this script if this is something that is performed regularly.  Feel free to comment below regarding any changes to the script you'd like to suggest.