Link to home
Start Free TrialLog in
Avatar of Aaron Margerum
Aaron Margerum

asked on

Exchange script to copy email from a users inbox for a specific date range?

Looking for a script that will accomplish the following task.

Copy email from a user’s mailbox that meets a specific date. The copy script should only execute based on the effective date plus 60 days. For example, if a daily script started running Oct 1st, no emails will be copied until Nov 30th. So on Nov 30th only emails from Oct 1st will be copied. On Dec 1st emails from Oct 2nd will be copied, so on an so forth.  No emails will be copied prior to the start date, so if the script starts running on Oct 1st, we can't copy any emails from Sept 30th.  

We would like to copy email from all mail folders except deleted items. We are currently running Exchange 2013.

Basically:  Copy email to "resource.pst" if Date = Current Date + 60 days

Is there are script or software that anyone can think of that can accomplish this task? Please let me know if there are any additional information that I need to provide or if I need to clarify anything.  Thank you.
Avatar of Alexei Kuznetsov
Alexei Kuznetsov
Flag of United States of America image

Copy email to "resource.pst" if Date = Current Date + 60 days
But Current Date + 60 days is always in the future! Please provide clarifications. For example, how many dates take place in this script? CurrentDate = Now, StartDate = X, ...?
Avatar of Aaron Margerum
Aaron Margerum

ASKER

Thank you for catching that, Alexei Kuznetsov I meant to type - 60.

The purpose of this script would be for archiving and accountability purposes.   The script will only be ran against certain users in a specially created group.  Users will be added and removed from the group as requested.  There will be a start date of when the script will start running.  Once the script starts running, only emails 60 days older than the Start date will be copied.  No emails can be copied from before the start date,  So if the daily script starts running on Oct 1st and User A is added on Oct 1st, the script will run daily but not actually start copying emails until Dec 1st.  Whereas is if User B is added on Dec 31st, the script will immediately start copying emails from Nov 1st.   The script would not go back and copy emails from Oct 1st.

Ideally the script would run on a nightly basis.   So we'd be concerned with the initial kick off date and items that are 60 days older than the current date, but not older than the kick off date.

I hope this clarifies things a little more.  Please let me know.
So, the script would copy (for a given User): all emails from specified StartDate to CurrentDate-60days, right? But you'll get duplicate emails each time you run the script because StartDate if fixed. CurrentDate will move to the future from day to day and you'll get dupes from StartDate to CurrentDate-1day (if run the script daily).
What version of Exchange?

If Exchange 2010 or newer, take a look at New-MailboxExportRequest and this reference.
Alexei Thank you for your response.  I don't believe that there would be any duplicates because the script would only copy items older than 59 days and younger than 61. So in essence if the requested start date is Oct 24th, The script wouldn't actually start copying emails until Dec 23rd and it would only copy emails dated Oct 24th. Then on Dec 24th it would copy emails dated Oct 25th. On Dec 25th, it would copy emails dated Oct 26t, etc.  If it's not possible to have a fixed date where we say , don't copy any emails before Oct 24th, then we'd just start running the script 60 days after the Oct 24th to avoid copying any emails prior to the start date.( I hope that makes more sense)
Todd, we are running Exchange 2013 CU 7.  Thank you for the links, I will take a look at them this evening.
Hmm. So, you only want to export mails for one day that is 60 days before the current day?
Todd thank you again for those links. I've seen the 2nd one.  I think that would be good if I we were doing a one time move for specific dates.  This needs to be ran on a daily basis with different variables.  If there were some way to add add the age variable instead of a date range, I think that could work.

The code from that site is as followed

New-MailboxExportRequest -ContentFilter {(Received -lt '04/01/2010') -and (Received -gt '03/01/2010')} -Mailbox "Pelton" -Name DPeltonExp -FilePath \\ExServer1\Imports\DPelton.pst

I'm in no way a scripting guru. I found this script below that looks like if it can be tweaked and merged the with the previous  script  may give me what I need:  ( May need to add the ExcludeFolders paramenter so it doenst copy things like calanedar, tasks, etc.)

$today=(get-date).adddays(-60)
$users = (Get-Content C:\Users\e003165\scripts\users\60DayOldMsgs\users.txt) <---- Amend path as needed.

foreach ($user in $users){
search-mailbox -identity $user -targetmailbox 'mbexportacct' -targetfolder 'Inbox' -searchquery "Received:< $($today)" |


I hope that I'm on the right track.  Open to anyone's thoughts.
Alexei, yes that is correct.  I know it sounds like an odd request but that is the request we received.
ASKER CERTIFIED SOLUTION
Avatar of Alexei Kuznetsov
Alexei Kuznetsov
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Alexei and Todd so much for your assistance in working through this solution.  I am going to hand this information over to my Messaging team so that they can try it out in their test environment.  I will post the results as soon as I hear back.  I wont abandon this solution.  Again, I really appreciate your help.
Aaron, can you give us an update of your progress and award as appropriate?