Delete mails

Hi,

Is there any way that i can delete a mail with specific subject from list of few user's mailbox from shell.
LVL 1
Manish KumarSr. Technical ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
You can use the powershell script below to accomplish this.

Put your Mailbox Usernames in a text file and then save it. Change the path / file name in the script to reflect your environment.  Also change the Subject Message.

$Mailboxes  = get-content "c:\filename.txt"
ForEach ($Mailbox in $Mailboxes) 
{
Export-Mailbox -Identity $Mailbox -PSTFolderPath "C:\exportedItems.pst" -BadItemLimit 1000000 -SubjectKeywords "CLICK HERE TO Upgrade" -DeleteContent -Confirm:$false
}

Open in new window


Will.
0
Manish KumarSr. Technical ConsultantAuthor Commented:
hi will,

Thanks a lot for this information.

1.I am working on exchange 2010 sp3. will Export-Mailbox command work ?
2. In text file i have saved alias of user mailboxes and separate it with semi colon, is that correct?
0
Will SzymkowskiSenior Solution ArchitectCommented:
1.I am working on exchange 2010 sp3. will Export-Mailbox command work ?

Sorry no you will need to use Search-Mailbox rather than Export-Mailbox.

2. In text file i have saved alias of user mailboxes and separate it with semi colon, is that correct?
No, what you need to do is have each alias on its own line. Example below

bsmith
mdrown
tmayer
etc...

Script Changed

You first need to run the below command to add the Export / Import role group to your account or a group you specify.  
New-ManagementRoleAssignment -Name "Import Export_Enterprise Support" -SecurityGroup "Enterprise Support" -Role "Mailbox Import Export"

Open in new window


Then you can run the following command...
$Mailboxes  = get-content "c:\filename.txt"
ForEach ($Mailbox in $Mailboxes) 
{
Search-Mailbox -Identity $Mailbox -SearchQuery "Subject:CLICK HERE TO Upgrade" -TargetMailbox "Mailbox Name" -TargetFolder "MailboxSearchDeleted" -DeleteContent -Confirm:$false
}

Open in new window


Will.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.