Adding a Contact to a Distribution List with PowerShell

The PowerShell ActiveDirectory module has a lot of great features that I use on a daily basis. However, there is one shortcoming that I have struggled with for a while. I did a lot of internet searching and testing to see if I was missing some hidden secret. But, alas, this is one task that the AD module does not do.

Here is the scenario. We have a lot of AD Groups (Distribution Lists) we use for notification messages. We want to send notifications to mobile devices. We do this by sending an email to the devices email address. For example:

2065551212@mobilecarrier.xyz.com

These external email addresses are created as Contact objects in AD.

The problem is that the cmdlets for managing AD group objects only allow you to add objects that have a SamAccountName (and therefore a SID) to a group. This is fine for user and group objects. But Contact objects to not have SIDs. So now what do you do.

The answer is you do it the old way you would have done it in VBScript; use ADSI.

$dlGroup = [adsi]'LDAP://CN=DL-Group Name,OU=Corp Distribution Lists,DC=contoso,DC=net'
$dlGroup.Member.Add('CN=mobile-username,OU=Corp Contacts,DC=contoso,DC=net')
$dlGroup.psbase.CommitChanges()

I am an experienced IT technologist specializing in optimizing user experiences, providing best-in-class support and developing creative solutions. I script therefore I am. I build tools to improve troubleshooting and gather supporting data.

Tagged with:
Posted in PowerShell
8 comments on “Adding a Contact to a Distribution List with PowerShell
  1. Anthony says:

    I’m hitting this issue now that we are upgrading from Exchange 2007 to 2013. This operation works fine with the Exchange PowerShell command Add/Remove-DistributionGroupMember, but because of split permissions, we now must the AD PowerShell command Add/Remove-ADGroupMember. I was thinking I may have to do this the old fashioned way, and you just confirmed it for me.
    Thanks,
    Anthony

    Like

  2. Roldy says:

    This is my approach using more conventional PowerShell cmdlets.

    $GroupMembers = @()
    Get-ADObject -Filter {Company -eq “Contoso”} | ForEach-Object {$GroupMembers += $_.DistinguishedName}
    Set-ADObject -Identity “CN=Contoso Group,OU=Groups,DC=contoso,DC=com” -Replace @{member=$GroupMembers}

    Like

  3. Potpal says:

    Yes, I meant to reply to Rody’s comment for his powershell approach. For your ADSI solution is there way to add multiple contacts with a CSV file? Like you I found that there isn’t a simple solution to add bulk contacts to a AD Groups. Any additional pointers appreciated.

    Like

    • Sean Wheeler says:

      Import-CSV “C:\Scripts\Contacts\ContactMembership.csv” | ForEach {
      $dlGroup = [adsi](‘LDAP://’ + $_.GroupCN
      $dlGroup.Member.Add($_.UserCN)
      $dlGroup.psbase.CommitChanges()
      }

      Like

  4. Potpal says:

    Hi Sean, I’m getting “Expected end of statement” error Line 1 Char: 12 code: 800A0401 when pasting above code into .vbs file. Which puts the char in question at the ” just before C:\ .

    To make sure my CSV file is correct I ran each line of your original code manually with the CN from the input file and it works wonderfully. Feel like we are almost there just not sure what am I missing to get the bulk import code working, appreciate your help.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

sean on it
Categories
Follow Sean on IT on WordPress.com
Blog Stats
  • 65,943 hits
Mike F Robbins

Scripting | Automation | Efficiency

%d bloggers like this: