Hi,
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:\DL\userlist.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:\DL\userlist.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