Script repository

Export mailbox to PST

Updated on: Jan 18, 2026, Views: 3579

Export data, Exchange

The scripts initiate and track exporting an on-premises Exchange mailbox to a PST file. To execute the script, create a business rule, custom command or scheduled task configured for the required object type.

Script 1: Initiate mailbox export

This script initiates mailbox export and saves the export request ID to a certain property of the target object so it would be possible to track the export request in future.

Parameters

  • $filePath - the path to the PST file that will be created by the script.
  • $requestIdProperty - the name of the target object property that will store export request ID.
$filePath = "\ServerShare%username%.pst" # TODO: modify me
$requestIdProperty = "adm-CustomAttributeText1" # TODO: modify me

# Create mailbox export request
$requestId = $Context.TargetObject.CreateMailboxExportRequest($filePath, 0, 0)

# Save request id
$Context.TargetObject.Put($requestIdProperty, $requestId)
$Context.TargetObject.SetInfo()

Script 2: Track mailbox export process

This script checks whether mailbox export is complete and sends an email message with errors that occurred during the export process (if any). To execute the script, create a custom command or scheduled task configured for the required object type.

Parameters

  • $requestIdProperty - the name of the target object property storing the export request ID. It must be the same property as specified in $requestIdProperty of Script 1.
  • $to - the address of the email notification recipient.
  • $subject - the subject of email notification.
  • $message - the text of the email notification. In the text, the {0} placeholder will be replaced with the mailbox name, and the {1} placeholder will be replaced with the export request failure message.
$requestIdProperty = "adm-CustomAttributeText1" # TODO: modify me

# Email settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Mailbox export failed" # TODO: modify me
$message = "
Mailbox: {0}
Failure Message: {1}" # TODO: modify me

# Get mailbox export request info.
$requestId = $Context.TargetObject.Get($requestIdProperty)
$requestInfo = $Context.TargetObject.GetMailboxExportRequestInfo($requestId, $False)

switch ($requestInfo.Status)
{
    "ADM_EXPORTMAILBOXSTATUS_INPROCESS"
    {
        return
    }
    "ADM_EXPORTMAILBOXSTATUS_FAILED"
    {
        # Send mail
        $message = [System.String]::Format($message, @($Context.GetDisplayNameFromAdsPath($Context.TargetObject.AdsPath), $requestInfo.FailureMessage))
        $Context.SendMail($to, $subject, $message, $NULL)

        # Remove export request ID.
        $Context.TargetObject.Put($requestIdProperty, $NULL)
        $Context.TargetObject.SetInfo()
    }
    "ADM_EXPORTMAILBOXSTATUS_SUCCEEDED"
    {
        # Remove export request ID.
        $Context.TargetObject.Put($requestIdProperty, $NULL)
        $Context.TargetObject.SetInfo()
    }
}

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.