Link to home
Start Free TrialLog in
Avatar of Éric Moreau
Éric MoreauFlag for Canada

asked on

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.
Avatar of aravind anche
aravind anche
Flag of United States of America image

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

Open in new window

Avatar of Éric Moreau


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?
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And by clear I mean remove items from specific date range :)
Avatar of Bill Prew
Bill Prew


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

The Force switch overrides the confirmation prompt displayed when your use the DeleteContent switch to permanently delete messages.
Bill, my mails are hosted in Office 365.
Vasil, isn't EWS deprecated?
Okay, I can't help with that, good luck.

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).
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.
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.
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.

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
User generated image
Configured the settings I desire
User generated image
Clicked on the Retention Policy Tab and created a new policy to apply the tags to
User generated image
Navigated to the Recipients Mailbox  and clicked on Mailbox Settings and selected Messaging Records Management
User generated image
Checked Apply Retention Policy and browsed for the one I wish to apply
User generated image
Selected the policy
User generated image
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.
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
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.
Can you confirm what license you have for Office 365?
  1. E3
  2. Business Premimum
  3. Proplus
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.
yo_bee, most users have business essentials, some have E1