Recent Exchange versions support a handy Journaling Mailbox for recording and storing all email messages that go in or out of the mail server for purposes such as audit and backup. Overtime, the Journaling Mailbox grows large and needs to be trimmed or pruned.
This article documents a PowerShell maintenance script I have written to report and automate the monthly archiving process of the Exchange 2013 Journaling Mailbox.
This script uses the PowerShell command New-MailboxExportRequest -Mailbox messagejournal to export Exchange 2013 Journaling Mailbox of previous month 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 (e.g. 2016-01-01 to 2016-01-31). It is designed to be run at the beginning of each month using Windows Task Scheduler.
Email Alerting, Reporting and Logging
A log file is created under script working 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. (Even so, log file should be manually checked to ensure so.)
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 2013 server where:
- System Locale is English (United States)
- Short date format (for en-US) is MM/dd/yyyy
Note: The script has not been tested in other configurations.
[Output Examples] section in the script below is based on this configuration.
(Suppose current date is 1 Feb 16; last month is Jan 16.)
Script Usage and Instructions
- Edit variable parameters as required (See sections 1-4 in the script below.)
- 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 Feb 2016 email, running the script any day within March 2016 (e.g. 1/Mar/16) would do, but beginning of month is recommended.
Setting up Windows Task Scheduler
- -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'"
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'"
PowerShell Script Download
- How can I move Exchange items to a PST using PowerShell?
- MailboxExportRequest ContentFilter is “Received -ne $null” when querying by date
- Cannot get Exchange PowerShell script to run under Scheduled Tasks