Exchange Batch Archive Import Script

The problem: You’ve just upgraded to Exchange 2010 or higher and / or enabled Online Archiving for your users. You’ve got a whole bunch of PST files to import for the users and no patience for typing in commands.

The solution: You need to use Powershell to run the imports, so you may as well script it. The below script was written for a small(ish) organisation where I wanted to import one user at a time, but they each had multiple PST files. When run, it will locate the folder for the user you specify, and queue each PST file for import into their Archive mailbox. The emails  from each PST will be placed into a folder named with the base name of the PST file (ie. excluding the ‘.pst’ file extension).

There’s a bit of preparation needed first, however;

  1. Create a folder on your file server (or wherever you plan to pull together your PST files), then create a subfolder within for each username for which you plan to import PSTs
    • The script will use the username you enter to find both the mailbox and the source folder, so make sure they are correct

archive structure

  1. Move each user’s PST files into their corresponding folder
  2. On the parent folder, add in the ‘Modify’ permission for ‘Exchange Trusted Subsystem’

  1. Share this folder on the network, ensuring that ‘Exchange Trusted Subsystem’ has at least modify permissions on the share
  2. Edit the script before running it, replacing the $folderpath variable with your shared folder’s UNC path
    • Note: The folder cannot be a local folder on your Exchange server, it needs to be a shared folder on the network.
  3. Open up the Exchange Management Shell and run the script from here by entering the full path and file name for where you have placed the script
    • eg. C:\temp\Batch_Archive_Import_v2.ps1

Note: If you run into issues running the script, you may need to enable permissions for your account to create import requests. More information is available here.

The script and download link are below.

# Script to import all PSTs in a specified folder, to the specified mailbox
# The script works on the premise that you have created a folder structure
# where each user has a folder based on their username, containing their
# archive PSTs. The parent folder needs 'modify' access to be granted to the
# AD object 'Exchange Trusted Subsystem'.

# This is primarily designed for new implementations of Online Archiving where
# all the old PSTs need to be imported. It is an interactive script which must
# be run from within an Exchange Management Shell window.

# Prompt user for destination mailbox username

$username = Read-Host 'Please enter the username of the destination mailbox'

# Generate folder path from username
# You will need to edit this folder path variable, specify

$folderpath = "\\fileserver01\archivepst\$username"

# Query folder to get names of PSTs, pass parameters to New-MailboxImportRequest command

Get-Childitem $folderpath\*.pst | ForEach-Object {
$jobname = '{0}-{1}-Import' -f $_.Name, $username
New-MailboxImportRequest -Mailbox $username -TargetRootFolder $_.BaseName -IsArchive -FilePath $_.FullName -BadItemLimit 50 -name $jobname

"`nPSTs queued, type command Get-MailboxImportRequest to check status`n"

Download link

Note: This script and instructions are provided as is, with no warranties or guarantees of any kind.

Leave a Reply

Your email address will not be published. Required fields are marked *