We help IT Professionals succeed at work.

Powershell script to delete items older than X days

861 Views
Last Modified: 2019-02-28
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.
Comment
Watch Question

aravind ancheWindows/Vmware
CERTIFIED EXPERT

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

Open in new window

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

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?
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2019

Commented:
And by clear I mean remove items from specific date range :)
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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
CERTIFIED EXPERT

Commented:
The Force switch overrides the confirmation prompt displayed when your use the DeleteContent switch to permanently delete messages.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
Bill, my mails are hosted in Office 365.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
Vasil, isn't EWS deprecated?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

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


»bp
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2019

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 Technology
CERTIFIED EXPERT

Commented:
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 Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

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 Technology
CERTIFIED EXPERT

Commented:
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 Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

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 Technology
CERTIFIED EXPERT

Commented:
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 Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

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 Technology
CERTIFIED EXPERT

Commented:
Can you confirm what license you have for Office 365?
  1. E3
  2. Business Premimum
  3. Proplus
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

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 Consultant
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
yo_bee, most users have business essentials, some have E1
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.