Avatar of Éric Moreau
Éric Moreau
Flag 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.
PowershellOutlookMicrosoft 365

Avatar of undefined
Last Comment
Éric Moreau

8/22/2022 - Mon
aravind anche

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

Open in new window

Éric Moreau

ASKER
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?
ASKER CERTIFIED SOLUTION
Vasil Michev (MVP)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Vasil Michev (MVP)

And by clear I mean remove items from specific date range :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Bill Prew

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 anche

The Force switch overrides the confirmation prompt displayed when your use the DeleteContent switch to permanently delete messages.
Éric Moreau

ASKER
Bill, my mails are hosted in Office 365.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Éric Moreau

ASKER
Vasil, isn't EWS deprecated?
Bill Prew

Okay, I can't help with that, good luck.


»bp
Vasil Michev (MVP)

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
yo_bee

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 Moreau

ASKER
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_bee

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Éric Moreau

ASKER
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_bee

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 Moreau

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
yo_bee

Can you confirm what license you have for Office 365?
  1. E3
  2. Business Premimum
  3. Proplus
Éric Moreau

ASKER
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 Moreau

ASKER
yo_bee, most users have business essentials, some have E1
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.