Powershell script to delete items older than X days

I have a lazy user who doesn't to empty his "deleted items" on a regular basis and doesn't want to use the "empty deleted items when closing Outlook" feature as he likes to delete older items and still keep some (ie keep last 2 months and delete everything older). This last trick wouldn't work if he is only using his phone/tablet for a while anyway.

Do you know which Powershell script I could use to delete emails from the "Deleted Items" older than 60 days?

I would schedule that script to run on a weekly basis and everybody would be happy.
LVL 72
Éric MoreauSenior .Net 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.

aravind ancheWindows/Vmware Commented:
Search-Mailbox -Identity $Username -SearchQuery "Received:<$('DD/MM/YYYY')" -DeleteContent

Open in new window

Éric MoreauSenior .Net ConsultantAuthor Commented:
thanks but I don't see anything that would limit to the "Deleted items" folder.

I have seen elsewhere that they are adding -Force after -DeleteContent. Why?
Vasil Michev (MVP)Commented:
You can use retention policies/tags for that, but you have to create a custom one, as few years back Microsoft changed the default policy to never delete items from the Deleted Items folder.

Or, you can use a custom EWS-based script to clear the folder. Here's a sample one: https://www.sunilchauhan.info/2015/09/remove-all-emails-from-specific-folder_17.html

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
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

Vasil Michev (MVP)Commented:
And by clear I mean remove items from specific date range :)
Bill PrewIT / Software Engineering ConsultantCommented:
Eric,

Did you want to run this on the Exchange server interacting with Exchange there, or on the users local computer interacting with Outlook there?


»bp
aravind ancheWindows/Vmware Commented:
The Force switch overrides the confirmation prompt displayed when your use the DeleteContent switch to permanently delete messages.
Éric MoreauSenior .Net ConsultantAuthor Commented:
Bill, my mails are hosted in Office 365.
Éric MoreauSenior .Net ConsultantAuthor Commented:
Vasil, isn't EWS deprecated?
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, I can't help with that, good luck.


»bp
Vasil Michev (MVP)Commented:
No, it isnt. And probably wont be deprecated anytime soon, there are at least dozen of migration apps out there that use it (including all tenant to tenant ones).
yo_beeDirector of Information TechnologyCommented:
I like Vasil Retention Policy method.  
I am not using o365, but I have to think it is the same.  I am currently using a custom policy that Deletes older than 7 and move to recovery.  The recovery is set on the system level as the default one.  

https://docs.microsoft.com/en-us/exchange/recipients/user-mailboxes/deleted-item-retention-and-recoverable-items-quotas?view=exchserver-2019&viewFallbackFrom=exchonline-ww
Éric MoreauSenior .Net ConsultantAuthor Commented:
yo_bee, I am full admin on my Office 365 tenant and I don't see all that details on the "mailbox usage" tab. I only see the last login and the usage. Nothing to adjust quota.
yo_beeDirector of Information TechnologyCommented:
Retention policy  is a background process that runs constantly. If you apply a retention policy against a mailbox the emails will be tagged and based on your policy you the action will be applied. There is no set date to select.  It is all based of todays date and how old the item is.
https://docs.microsoft.com/en-us/office365/securitycompliance/retention-policies

Quota's are meant to cap users and warn them when they are close to the quota.  

I am running an older On-Prem exchange server and I have a custom policy called Trash Pickup for items older then 7 days.  This tags all deleted items to be deleted from the Deleted Folder, but does not permanently delete the items.  They will remain in the recovery bin for the default set time (14 days).  

This is what you need to do for this one mailbox or maybe make it a company wide policy.  

I use the analogy of your garbage pickup for your house.  You don't put items in the trash bin and expect to hold on to them forever.  It's called the trash bin for a reason. I explain if you have emails you want do not use the trash bin as a folder.  

This will not look like yours, but it will be the same principal to follow.
I am opening this up using EMC, but it can be done with Powershell as well.

I nagivgated to the Organization Config container and clicked on Mailbox
Click on the Retention Policy Tag tab to create a new policy
reten1.jpg
Configured the settings I desire
reten2.jpg
Clicked on the Retention Policy Tab and created a new policy to apply the tags to
reten3.jpg
Navigated to the Recipients Mailbox  and clicked on Mailbox Settings and selected Messaging Records Management
reten4.jpg
Checked Apply Retention Policy and browsed for the one I wish to apply
reten5.jpg
Selected the policy
reten6.jpg
Éric MoreauSenior .Net ConsultantAuthor Commented:
I don't have the same thing in Office 365.

I am in the middle of writing some code on which I will have (hopefully) the full control over what I am deleting.
yo_beeDirector of Information TechnologyCommented:
what about the link i posted in my last reply.  
What License of O 365 do you have? I am trying to find the comparison sheet
Éric MoreauSenior .Net ConsultantAuthor Commented:
I have quickly checked the link. I have no retention policies set so far.

But I am currently testing the C# code I wrote based on Vasil/Sunil ideas. I will be able to have full flexibility over which mailboxes/folders/number of days that I want to keep.

I will surely be able to close the question later today.
yo_beeDirector of Information TechnologyCommented:
Can you confirm what license you have for Office 365?
  1. E3
  2. Business Premimum
  3. Proplus
Éric MoreauSenior .Net ConsultantAuthor Commented:
So finally I wrote some C# code using EWS as Vasil suggested and built a tool that will run on a weekly basis to clean exactly what I want.
Éric MoreauSenior .Net ConsultantAuthor Commented:
yo_bee, most users have business essentials, some have E1
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
Powershell

From novice to tech pro — start learning today.