Microsoft Exchange 2013 Automated PST Import Script

Microsoft Exchange

I needed to have an automated way to bulk import a load of PST files into Exchange 2013. In my case all the files were named as follows: samAccountName@domain.co.uk so for example: fred.bloggs@thingy.co.uk.pst

You can run this command from an Exchange PowerShell console, bear in mind that you’ll need to change your domain name on the New-MailboxImportRequest command as you need to change the FilePath too to match the place were the PST files are for import. Note this must be a UNC path, even if they are stored locally on the Exchange server.

You, if you haven’t done it already need to grant the user that will be running the task the rights to perform an import or export, to do this run this command, note you only need to do this once.

New-ManagementRoleAssignment –Role “Mailbox Import Export” –User "DOMAIN\USER"
New-ManagementRoleAssignment –Role “Mailbox Import Export” –SecurityGroup "DOMAIN\GROUP"

So here is the script, pop the files in the location, adjust the DOMAIN and filepath to match what you need it to be then you can run it from the Exchange 2013 PowerShell console or from the PowerShell ISE.

### AutomatedMailboxImport.ps1

### This script automatically imports PST files into a users mailbox based on the name of the file named

# as the username.

# Uncomment this line for testing within the Windows PowerShell ISE.

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

# initialize the items variable with the contents of a directory

$items = Get-ChildItem -Path "g:\import" -Filter *.pst

# Print to the screen, what this script is now doing.

Write-Host "Importing PST files into Exchange Mailboxes"

Write-Host

# enumerate the items array

foreach ($item in $items)

{

      # if the item is not a directory, then process it.

      if ($item.Attributes -ne "Directory")

      {

            Write-Host "Processing the file: $item"

            $importfilename = $item.Name

           

            # If it contains a substring of @ then we want to chop this off to create the username.

            if ($item.Name -match '@') {

                # the filename contains a '@' we want to get only the first part of the filename as the username.

                $importusername = ($item.basename -split '@')[0]

            } else {

                # the filename didn't contain a '@', so we can just use the file base name.

                $importusername = $item.BaseName

            }

            Write-Host "Import Filename: $importfilename"

            Write-Host "Username: $importusername"

            New-MailboxImportRequest -Mailbox "DOMAIN\$importusername" -FilePath “\\SERVER\g$\import\$importfilename" -BadItemLimit Unlimited –AcceptLargeDataLoss

      }

}

Once you’ve run it, it will have queued up all the imports, you can look at their current state of import with:

get-mailboximportrequest | get-mailboximportrequeststatistics

Once they have all been imported, you can clean them up with:

Get-MailboxImportRequest -Status Completed | Remove-MailboxImportRequest

Leave a Reply

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