If we want to import PST files into an Exchange mailbox, we can use the PowerShell CMDlet New-MailboxImportRequest. The PST files that we want to import must be available through a file share, because the CMDlets only accept UNC paths. To import a PST file, we use the following command:
New-MailboxImportRequest -Mailbox 'Name of Mailbox' -FilePath 'UNC-path of the PST-file'
To show this in a real example:
New-MailboxImportRequest -Mailbox drpe -FilePath \\ExC2019\transfer\outlook1.pst
With this command we import the complete contents of the PST file. However, If we use the -verbose option, we will get more information during the import and it helps to find errors faster.
If we receive an error, we can use the Get-ManagementRoleAssignment -RoleAssignee <group or user> command to verify that the user with whom we are running the command has the Mailbox Import Export rights. In addition, the appropriate destination mailbox must exist. We can verify this behavior by using the get-mailbox -Identity <name> command.
With the command get-mailbox -identity <name> | get-mailboxpermission we can verify that we have sufficient rights on the mailbox. The two CMDlets Get-MailboxImportRequest and Get-MailboxImportRequestStatistics provide real-time information about import processes. With these CMDlets we can also use pipes to get more detailed information, like the current status of an import request , its progress in percent and even to get a complete report for a request.
Get-MailboxImportRequest 'Name of import request| fl
Get-MailboxImportRequest 'Name of import request| Get-MailboxImportRequestStatistics
With this, you can easily build a “do…until” loop which waits until the import request has completed and then executes further actions.
In the following example, the do until loop checks and shows the current status of a request every 5 seconds and will send a notification e-mail, including the request report once it has completed.
$reqname = 'Name of import request'
$report = 'path for report file to be saved to'
$reqstat = Get-MailboxImportRequest $reqname |Get-MailboxImportRequestStatistics
"Import request $reqname is running. Percent completed: $($status.PercentComplete)%"
sleep -Seconds 5
until($reqstat.Status -eq "Completed" -or $reqstat.Status -eq "Failed" -or $reqstat.Status -eq "CompletedWithWarning")
#save the request report to file
Get-MailboxImportRequest $reqname | Get-MailboxImportRequestStatistics -IncludeReport | fl > $report
#send an email with status info and the request report as attachement
Send-MailMessage -Body "Import request $reqname finished with status $($reqstat.Status)." -From 'sender email' -to 'recipient email' -SmtpServer 'smtp server address' -Subject "PST import completed" -Attachments $report
After an import request has been completed, we need to delete the entry of this import request. To do so, we use the CMDlet Remove-MailboxImportRequest.
By the way, in addition to the ability to import a complete PST file, we can also select only individual folders to be imported. This is done by using the -IncludeFolders parameter. Only the specified folders will be imported and all other folders will be ignored from the import. Following, the complete command.
New-MailboxImportRequest -Mailbox 'name' -FilePath 'UNC-path of the PST-file' -IncludeFolders 'Name of the folder inside the PST-file'
If we want to import all folders and omit individual ones, we can use the -ExcludeFolders option. With the option -ExcludeDumpster we exclude the recycle bin from the import.
However, we can also specify the destination folder in the mailbox where the CMDlet should import the data:
New-MailboxImportRequest -Mailbox 'name' -FilePath 'UNC-path of the PST-file' -TargetRootFolder 'Folder in mailbox'
If the specified folder is not present inside of the mailbox, it will automatically be created by the CMDlet. The IsArchive switch will make the wizard import the PST file into the user’s archive.