Exchange 2013 Script to remove Resigned Users from all the DL’s in organization

Tags:
Microsoft Exchange 2013
I am trying to write a script that will do the following tasks:
  1. Members of DL named "Resigned" to be removed from all the other DL's in the organization except DL "Resigned".
  2. Later I need to disable/remove the users based on their RecipientType.
Complete Details : Some organizations end up having cluttered distribution lists still populated with mailboxes belonging to employees who have left the company. One strategy is to put the email ID's of those employees (mailbox/mail-contacts) to a Distribution List called “Resigned”. That list contains all non-active employees whose mailboxes have not been deleted yet. 1) Get All the Distribution Groups from Exchange except DL named "Resigned" in parameter called $allgroups: $allgroups=get-distributiongroup -Filter {"DisplayName" -ne "Resigned"} 2) Remove the employees who are listed in the DL named "Resigned" from all other DL's in Exchange : get-distributiongroupmember "Resigned" | remove-distributiongroupmember $allgroups 3) Since we have the required email ID's already in DL Named "Resigned" we need to disable/remove them based on their Recipient Type. If the email ID is  usermailbox then task is to disable the mailbox and if its a mailcontact task is to remove the contact. $EmailIDs= get-distributiongroupmember -identity "Resigned" | export-txt "D:DLuserlist.txt" foreach($EmailID in $EmailIDs) { If($EmailID -RecipientType -eq "UserMailbox") { get-mailbox $EmailID | Disable-Mailbox $EmailID } Else($EmailID -RecipientType -eq "MailContact") { get-mailcontact $EmailID | Remove-MailContact $EmailID } } PowerShell Script : Till now the powershell script I have reached is as follows: $allgroups=get-distributiongroup -Filter {"DisplayName" -ne "Resigned"} get-distributiongroupmember "Resigned" | remove-distributiongroupmember $allgroups $EmailIDs= get-distributiongroupmember -identity "Resigned" | export-txt "D:DLuserlist.txt" foreach($EmailID in $EmailIDs) { If($EmailID -RecipientType -eq "UserMailbox") { get-mailbox $EmailID | Disable-Mailbox $EmailID } Else($EmailID -RecipientType -eq "MailContact") { get-mailcontact $EmailID | Remove-MailContact $EmailID } } Lot of errors in the above script but I'm sure I will get my answers from the Experts on this website. Please Help
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: