Bulk Importing of PST Files via PowerShell

Need to import 90 PST files because you're removing users' abilities to be able to mount them and use them? Yeah, I had to, too.

Sadly, these PST files were found after I had collapsed and removed our PST Capture setup... Timing, right?!

Either way, there really is no need to reconfigure PST Capture in order to import this one user's archives. Let's grant ourselves Import/Export role in Exchange with PowerShell.

New-ManagementRoleAssignment -Role "Mailbox Import Export" -User mikeRiston


Okay, now that we have that sorted out, let's get these imported!

We're going to be utilizing the Exchange Management Shell and the New-MailboxImportRequest cmdlet. It'll look fairly similar to this:

New-MailboxImportRequest -Mailbox $target_user -FilePath $pathtoPST -IsArchive -Name $MyJobName

Here is the full mini-script I wrote up to add a little logging and generate some useful names to my import jobs.


#Get our PSTs
$path = Read-Host "Where are the .PST files stored?"
$psts = gci $path\*pst -Recurse

#Count the # of PSTs
$count = $psts.count

#Define our target user
$target_user = Read-Host Who is the user?

#Get our target user email account
$target_user_email = get-mailbox $target_user

#Start a loop to import
foreach ($pst in $psts) {
    Write-Host Now Importing $pst.FullName
    $pstpath = $pst.FullName
    $jobname = "Bruce_"+$pst.name+"_"+$count
    
    
    #Create the actual request
    New-MailboxImportRequest -Mailbox $target_user -FilePath $pstpath -IsArchive -Name $jobname 
    
    #Start check for job completion - only processing one at a time
    while ( `
        (Get-MailboxImportRequest -Name $jobname | Get-MailboxImportRequestStatistics).Status -ne "Completed" ) {
            Write-Host Currently Processing $pstpath -ForegroundColor DarkBlue -BackgroundColor DarkYellow
            start-sleep -Seconds 5
            }

    #Delay a little while for everything to close out our $pst
    Start-Sleep -Seconds 10

    #Rename our file to mark it completed
    $newname = $pst.FullName +".done"
    Rename-Item $pst.FullName $newname -Force

    #Announce file completion
    Write-Host Finsihed importing $pst.Name -BackgroundColor Black -ForegroundColor Green
    
    #decrement counter
    $count--
    
    #Announce files remaining
    Write-Host $count items remaining
    } 

Comments

Popular Posts