Exchange 2013/16 Journal Mailbox Archive-to-PST PowerShell Script Automation with Reporting
Overtime, the Exchange journal mailbox grows so large and needs to be trimmed or pruned. This script was written to automate the process.
Recent Exchange versions have built-in support of journaling for recording all inbound and outbound email messages for backup or compliance reasons. Overtime, the journal mailbox grows so large and needs to be trimmed or pruned.
This article documents a PowerShell maintenance script written for reporting and automating the monthly archive-to-PST process of the Exchange 2013 journaling mailbox. (Based on reader feedback, it also suits Exchange 2016 Standard.)
[Go to Download]
Archiving Concept
This script uses the PowerShell cmdlet New-MailboxExportRequest -Mailbox <journal mailbox>
to export Exchange Journaling Mailbox of previous month (e.g. 2016–01–01 to 2016–01–31) as a standard PST file (e.g. archive 2016_01_31.pst) to specified locations (up to two locations) and then uses Search-Mailbox -DeleteContent
to delete email messages within the date range if successful. It is designed to be run at the beginning of each month (e.g. 2/Feb/16) using Windows Task Scheduler.
Email Alerting, Reporting and Logging
A log file is created under a specified directory during each execution. An email is sent to specified email addresses with the log file when done. The email message subject indicates COMPLETE/FAILURE.
In addition, a list of email messages returned from search and deleted by Search-Mailbox
cmdlet will be available as zip(csv) attachments in another specified mailbox and folder as mentioned in the message body of above email.
Safety Measure to Prevent Accidental Email Deletion
In case mail archiving has failed, script will send an alert mail and exit so that no mail deletion will occur. In technical terms, when status of Get-MailboxExportRequest
is not “Completed”, script keeps on waiting (looping) until it is complete. If the loop is broken or script receives any status other than “Completed”, execution will be terminated and failure will be reported by email.
Assumptions and Requirements
PowerShell 3.0 or above
Execute this script on Exchange server where (preferably):
- System Locale is English (United States)
- Short date format (for en-US) is MM/dd/yyyySufficient disk space for storing PST files in the specified location(s)
Note: This script was designed and tested for email messages of previous month, and has not been tested in other configurations but could still work with some tweaking. (In fact, it does not only work for journal mailbox.)
Getting Started
Script Usage and Instructions
Edit variable parameters as required ( Refer to the script comment sections 1–4 )
Save the script in a local folder e.g. C:\scripts\ws_email_archiving_script.ps1
To export email of last month, the script should be scheduled to run any day within the current month.
For example, to export Jan 2016 email, running the script any day within Feb 2016 would do; the beginning of month is recommended (e.g. 2/Feb/16) .
Setting up Windows Task Scheduler
Program/script:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments:
-PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V15\Bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\Bin\Exchange.ps1'; &'C:\scripts\ws_email_archiving_script.ps1'"
Start in:
C:\Scripts
Alternatively, start the .ps1 script using a .bat script:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V15\Bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\Bin\Exchange.ps1'; &'C:\scripts\ws_email_archiving_script.ps1'"
Download
For download of the latest version of ws-email-archiving
, visit its homepage.
Word of Mouth
Below are some of the kind comments left by users of this script. (Thanks!)
“We have some very large journaling mailboxes that we’ve been racking our brains trying to figure out what to do with to stop the sprawl. We’re on Exchange 2016 Standard, so we’re limited on our database count. This script has made it really easy for us to dig out emails by month and save them off to PST that we can store out on second tier storage for archive. Thanks for this!”
“I’ve used your script for a couple of years. It’s GREAT and do a GREAT job. Thank you very much!”
Release History
Welcome to buy me a cup of coffee ☕ if this tool is useful to you. 😊 Thanks!
If you would love the script to be further developed, you may engage professional services on one of the freelancing platforms.
Originally published at https://tech.wandersick.com on July 3, 2016.