July 3, 2016

Exchange 2013 Journal Mailbox Archive-to-PST PowerShell Script Automation with Reporting

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 I have written for a customer to report and automate the monthly archive-to-PST process of the Exchange 2013 journaling mailbox.

Archiving Concept

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 but could still work.

[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

  1. Edit variable parameters as required (See sections 1-4 in the script below.)
  2. Save the script in a local folder e.g. C:\scripts\ws_email_archiving_script.ps1
  3. 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


  • 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'"

PowerShell Script Download


  1. How can I move Exchange items to a PST using PowerShell?

  2. MailboxExportRequest ContentFilter is “Received -ne $null” when querying by date

  3. Cannot get Exchange PowerShell script to run under Scheduled Tasks

1 comment :

  1. Great Idea! thanks for that, How i can change this for take daily backups?