Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exchange 2013, Powershell command to delete email older than a date

Posted on 2013-11-17
12
Medium Priority
?
9,557 Views
Last Modified: 2013-11-26
Can anyone supply me with a script/command to delete all emails, calendars, contacts and everything else in all users mailboxes older than a certain date. This is for all users in an exchange 2013 environment.
The reason for this is we recently imported all email from another system/domain as part of a project and all email has appeared as unread (including calendars). This is a great inconvenience to the site.
The method for migrating the email is to export to pst from the old system and import in the new using powershell, the initial export is where we found the issue occurred with setting all email to unread.
We have now figured out how to export the email correctly without appearing as unread and saving the state from the previous system, but if we try a new-mailboximportrequest with the pst now it detects the email is already in there and doesn't copy over the top. And if we try a manual import of the pst through outlook setting "replace duplicates with items imported" it just goes and creates a whole bunch of duplicates.

Once we have the command to delete the email older than a certain date we will run the import again with the pst files that now have the correct state applied.

However if anyone can supply me with an alternative to the above, eg software which can import the pst over the top and correct the state that would be even better.
0
Comment
Question by:CodeBlueEngineers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 1500 total points
ID: 39655299
Use the following command below which will delete all email from all mailboxes in your Exchange environment up until November 1st 2013.

get-mailbox -resultsize "unlimited" | search-mailbox -SearchQuery "Received:<$(11/01/2013')" -deletecontent

Open in new window


Will.
0
 

Author Comment

by:CodeBlueEngineers
ID: 39655303
Brilliant, thanks for that, also can you provide the command for deleting from a single user, just so I can test it out
0
 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 1500 total points
ID: 39655313
all you need to do for that is the following...

search-mailbox -identity <username> -SearchQuery "Received:<$(11/01/2013')" -deletecontent

Open in new window


Will.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CodeBlueEngineers
ID: 39655329
Fantastic, and will that also delete contacts, calendars and tasks etc
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 39655334
This will be mailbox items. I have only tested this in my lab so i do not know about the contacts/calendars. You will just have to test it.

Will.
0
 

Author Comment

by:CodeBlueEngineers
ID: 39655580
Hi Will
After running the command I get another cursor line example

get-mailbox -resultsize "unlimited" | search-mailbox -SearchQuery "Received:<$(11/01/2013')" -deletecontent

then push enter and I get this
>>
Not quite sure what to put there
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 39656196
Sorry, there should be a single quote ' before the 11 in the date. That should do it.

Will.
0
 

Accepted Solution

by:
CodeBlueEngineers earned 0 total points
ID: 39660558
Gave it a go, it didn't seem to pick up any results. We have sorted the issue now by exporting all email after the migration (new email) deleting the content from theie mailbox and importing the pst from the old system and the importing the pst which contains the mail after the migration. Using the below commands

Export mail after the migration to pst between 15-11-13 and 21-11-13 (to get this command to work we had to set date into US format)
New-MailboxExportRequest -ContentFilter {(Received -lt '11/21/2013') -and (Received -gt '11/15/2013') -or (Sent -lt '11/21/2013') -and (Sent -ge '11/15/2013')} -Mailbox <username>  –FilePath “\\Server01\e$\Exports\<username>.pst”

Delete content from the mailbox
search-mailbox -identity <identity> -deletecontent

Import email from the new pst (change the folderpath as per where the pst is kept)
Dir \\server01\foldername\*.pst | %{ New-MailboxImportRequest -Name SmallImport -BatchName Recovered -largeitemlimit 50 -Mailbox $_.BaseName -FilePath $_.FullName}

This seemed to work in the end, however I have noticed that some of the users have some duplicate folders in outlook. eg there is now another contacts folder called contacts1 and another folder containing tasks called tasks1. These folders contain no data
Do you know the command to run to search the users mailbox and delete these folders?
The command for 1 user and the command for 2 users would be great thanks
0
 

Author Comment

by:CodeBlueEngineers
ID: 39660580
sorry at the bottom I meant to say the command for 1 user and the command for all users
0
 

Author Comment

by:CodeBlueEngineers
ID: 39667945
I've requested that this question be closed as follows:

Accepted answer: 0 points for CodeBlueEngineers's comment #a39660558

for the following reason:

The above commands are what helped us workaround the issue
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 39667946
If this solution help get to the end result the points should be awarded fairly.

Will.
0
 

Author Closing Comment

by:CodeBlueEngineers
ID: 39677111
This is what fixed the issue
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
Are you an Exchange administrator employed with an organization? And, have you encountered a corrupt Exchange database due to which you are not able to open its EDB file. This article will explain all the steps to repair corrupt Exchange database.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question