Bulk import PST files – [updated] v1.1

Just wrote a basic script for importing PST files into mailboxes, that I want to share with the community.

It is designed to check for PST files in the specified folder. Based on the filenames (of the PST files) it then verifies that a mailbox can be found. This is done by using the filename and adding the @ character and the domain value into a string value. If there is a match, it returns a value of $True and the script continues with running the New-MailboxImportRequest cmdlet.

The script is written just as basic as it can, it provides much information about values and what’s going on.
The most recent updated script can be downloaded here

I hope this will help you to import the PST files into the mailboxes

Ps. Sorry for the word-wrap, see the script file instead of copy the script code below

Changelog
v1.1
– Updated the $name variable due to issues with filenames got trimmed away. Also added so that if errors exists, they will be sent to a errorlog. Thanks to Chris Steding!

# +=======================================================================
# | Blog: http://www.testlabs.se/blog
# | Twitter: @jonand82
# | =============================================
# | Filename: Import-PST v1.1.ps1
# |
# | CREATED BY: Jonas Andersson
# | FUNCTION: Imports PST files into mailboxes, matching on emailaddresses
# |
# | CHANGE LOG: 
# | v1.0 - 2013-09-18, *Created*
# | v1.1 - 2013-09-22, *Update of $name variable*
# |
# |    Required permissions (RBAC) Role: “Mailbox Import Export”, example: New-Managementroleassignment –Role “Mailbox Import Export” –User “Administrator”
# +=======================================================================

# Load snapin
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction 'SilentlyContinue'

# Variables
$error.clear()
$errorlog = "C:\temp\errorlog.txt"
$pstpath = "C:\temp"
$domain = "testlabs.se"
$servername = "tlex01"
$files = Get-ChildItem -Path $pstpath -Filter *.pst

Write-Host $files

if (($files -ne $null) -or ($files -eq "")) {

    foreach ($i in $files) {

        $name = $i.BaseName
        $id = $name + "@" + $domain
        $filename = $i.FullName
        $filename = $filename.Replace(":","$")
        $uncfilepath = "\\" + $servername + "\" + $filename

        Write-Host "#################################################################"
        Write-Host "Filename:" $i -ForegroundColor 'Cyan'
        Write-Host "UNC path:" $uncfilepath -ForegroundColor 'DarkCyan'
        Write-Host "Emailaddress:" $id -ForegroundColor 'DarkGreen'

        $MailboxExists = [bool](Get-Mailbox -Identity $id -ErrorAction SilentlyContinue)

        if ($MailboxExists -eq $false)
        {
            Write-Host "Found mailbox:" $MailboxExists -ForegroundColor 'Red'
            Write-Host "Make sure to match filename to mailaddress, without @domain" -ForegroundColor 'Red'

        }

        if ($MailboxExists -eq $true)
        {
            Write-Host "Found mailbox:" $MailboxExists -ForegroundColor 'Green'
            Write-Host "Importing $uncfilepath into mailbox: $id" -ForegroundColor 'White'

            New-MailboxImportRequest -Mailbox $id -FilePath $uncfilepath

        }

        Write-Host ""
    }

}

else
{
    Write-Host "No PST files found"
}

if ($error -ne $null)
{
    $error | Out-File -FilePath $errorlog -Append
    Write-Host "See $errorlog for errors" -ForegroundColor 'Red'
}