PS script for deleting mail Exchange 2010

Hi all;

I'm looking for a PS script that will delete an email sent by mistake to specific users. The goal here is NOT to query all mailboxes. I'm looking for a pre-canned script that I can edit the variables(subject, date sent), while pulling a list of users from a text file when needed "A text list would look something like (aliases or full display name); could do by DL using get-adgroupmember –recursive (an example would be appreciated!) to populate the text.  We also would need to run this as an account that has full access to all mailboxes, so we need to put that right in script.

Would the following work:

Get-Content “C:\input.txt” | Get-Mailbox | Add-MailboxPermission –AccessRights FullAccess –User –AutoMapping:$false
Get-Content “C:\input.txt” | Get-Mailbox | Search-Mailbox -SearchQuery “Subject: Executive Bonus Sent:5/14/2015″ -DeleteContent -TargetMailbox “Restore-Mailbox” -TargetFolder “inbox” -loglevel full
Get-Content “C:\input.txt” | Get-Mailbox | Remove-MailboxPermission –AccessRights FullAccess –User

Thank you.
Kinan Al-HaffarSenior Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:
The script above will work for your but you do have some syntax issues. . I have basically taken what you have already done and just put in variables. I have also wrapped this in a foreach loop.

$users = get-content "c:\filename.txt"
$date = "5/14/2015"
ForEach ($user in $users) {
Add-MailboxPermission -Identity $user Add-MailboxPermission –AccessRights FullAccess –User –AutoMapping:$false
Search-Mailbox -Identity $user -SearchQuery “Subject: Executive Bonus Sent:$date -DeleteContent -TargetMailbox “Restore-Mailbox” -TargetFolder “inbox” -loglevel full
Remove-MailboxPermission -Identity $user –AccessRights FullAccess –User

Open in new window


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

From novice to tech pro — start learning today.