Remove Contacts from Distribution Groups with PowerShell

I am currently the administrator of our cloud Exchange 365 server, and I am having no luck automating the removal of contacts from distribution lists. We have clients all over the country and our distribution lists change frequently. I wanted to write a powershell script that would pull email addresses from a CSV, link the addresses to their AD contact counterpart, and then remove them all from a pre-specified distribution group.

It seemed like this would be easy with the 'Remove-DistributionGroupMember' exchange command, but I can't use it as our AD is synced with Exchange so all changes have to be made in Active Directory. I am very new to PowerShell, and every attempt I've made has failed.

Does someone know of a command to remove a contact from a specified distribution group? I've made multiple attempts (like below), but in all honesty, I am just not good enough in PowerShell to get this.

$dn = Get-ADObject -filter {mail -eq "EMAIL@ADDRESS.COM"}

Remove-ADGroupMember GROUP $dn.distinguishedname

ERROR:

Remove-ADGroupMember : Cannot find an object with identity: 'CN=DelContact,OU=
Contacts,DC=domain,DC=com' under: 'DC=domain,DC=com'.
At line:1 char:21
+ Remove-ADGroupMember <<<<  GROUP $dn.distinguishedname
    + CategoryInfo          : ObjectNotFound: (CN=DelContact,...C=domain,DC=com
   :ADPrincipal) [Remove-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Micros
   oft.ActiveDirectory.Management.Commands.RemoveADGroupMember

Thanks in advance!


  • Edited by ninjajuice15 Friday, March 27, 2015 5:51 PM title was incorrect
March 27th, 2015 5:46pm

Thank you so much! That worked perfectly.
Free Windows Admin Tool Kit Click here and download it now
March 30th, 2015 11:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics