PST

Bulk import PST files – [updated] v1.1

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'
}
Search for PST files on clients

Search for PST files on clients

Wrote a basic PowerShell script that searches all computers listed in the clients.txt file.

Saving the pst files into the destination path.

For each computer, a folder is created with the corresponding computer name so that the PST files is collected per client.

Before the PST Capture Tool was released I wrote a post about “How to consolidate PST” files.
This script is just a little adjustment of that one.

Also have a look at the PST Capture Tool, it can be found here.

Anyway, feel free to use the script below and comment on it

#############################################################################
# Search.ps1
# Description:
# This PowerShell script searches for *.pst files on the clients listed in
# the clients.txt file (one hostname per row). Then saves them on the path specified
# in $SavePath, one folder per computer is created in the destionationpath.
#
# Jonas Andersson, MCC 2011
# http://www.testlabs.se/blog
# Twitter @jonand82
#############################################################################

$data = Get-Content .\clients.txt
$Date = Get-Date -Format yyyyMMddHHmm
$Log = "log_{0:yyyyMMdd-HHmm}.txt" -f (Get-Date)

#Writes to log file
Function WriteLog ([string] $sLogMsg) {
$sLogLine=(get-date).Tostring("yyyy-MM-dd-HHmm")+","+$sLogMsg
$sLogLine| out-file $log -noclobber -append
}

foreach ($row in $data)
{
WriteLog "Starting the search for PST files on $row."
$FindPath = "\\$row\c$"
$SavePath = "\\destionaionpath\pstshare"

$PSTFiles = Get-ChildItem -Path $FindPath -Recurse -Filter "*.pst"

if ($PSTFiles.count -eq $null) {
WriteLog "No PST files were found on computer $row."
}
else {
$PSTFiles | New-Item -path $SavePath -name $row -itemtype directory -force
$PSTFiles | Copy-Item -Destination "$SavePath\$row"
WriteLog "PST files was found and copied successfully on computer $row."
}
}

WriteLog "Script completed!"

Download link

Consolidate PST files

Consolidate PST files

Published: 2012-01-25
Updated: –
Version: 1.0

A time ago I was developing a PowerShell script for finding all PST files on a drive and copying them to a folder.
Type in a drive that should be searched and the UNC path to save them on
The script have been tested and verified on Exchange 2010 SP2

They are divided into two separate scripts.

Feel free to give feedback on it.

It can be downloaded here.


#############################################################################
# Search&Copy-PSTFiles.ps1
# Description
# This PowerShell script searches for *.pst files and copies them into the
# specified UNC location for the final step, the import of the pst contents
# into the associated mailboxes.
#
#
# Jonas Andersson, MCC 2011
# http://www.testlabs.se/blog
# Twitter @jonand82
#############################################################################

# Find PST Files

#Date
$date = Get-Date
$filename = "PST_Files_{0:yyyyMMdd-HHmm}.csv" -f (Get-Date)
$log = "log_{0:yyyyMMdd-HHmm}.txt" -f (Get-Date)
# End of variables

#Functions
#Searching for PST files

Function Search ($PST) {

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$drive = [Microsoft.VisualBasic.Interaction]::InputBox("Which drive do you want to search? (C:\)", "Drive (C:\)", "C:\")

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$unc = [Microsoft.VisualBasic.Interaction]::InputBox("Where do you want to save the PST files? UNC path for the Fileshare (\\server\share)", "UNC path", "Example: \\server\share")

WriteLog "Starting the search for PST files.."

$PSTFiles = Get-ChildItem -Path $drive -Recurse -Filter "*.pst"

if ($PSTFiles.count -eq $null){
WriteLog "No PST files were found.."
}
else{
$PSTFiles | Select-Object Name,Length,Directory | Out-File $log -append
$PSTFiles | Copy-Item -Destination $unc | Out-File $log -append
$PSTFiles | Select-Object Name,Length,Directory | Export-CSV -path $filename -Encoding Unicode -NoTypeInformation
WriteLog "PST files was found and copied successfully."
}
}

#Writes to log file
Function WriteLog ([string] $sLogMsg) {
$sLogLine=(get-date).Tostring("yyyy-MM-dd-HHmm")+","+$sLogMsg
$sLogLine| out-file $log -noclobber -append
}

#Script starts here
Search
WriteLog "Script completed!"

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show("Script completed!", "Information", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)

End of script

The import script starts below


#############################################################################
# Import-PSTFiles.ps1
# Description
# This is the second part of the script that does the actual import of the
# pst contents into the associated mailboxes.
# Filenames is matched with the Alias value on the mailboxes.
#
#
# Jonas Andersson, MCC 2011
# http://www.testlabs.se/blog
# Twitter @jonand82
#############################################################################

# Find PST Files

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

#Date
$date = Get-Date
$filename = "PST_Files_{0:yyyyMMdd-HHmm}.csv" -f (Get-Date)
$log = "log_import_{0:yyyyMMdd-HHmm}.txt" -f (Get-Date)

# End of variables

#Functions
#Searching for PST files

Function Import ($PST) {

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$path = [Microsoft.VisualBasic.Interaction]::InputBox("Where are the PST files stored? UNC path for the Fileshare (\\server\share)", "UNC path", "Example: \\server\share")

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$archive = [Microsoft.VisualBasic.Interaction]::MsgBox("Should they be imported to the archive mailbox?",'YesNo,Question', "Import to archive?")

WriteLog "Starting the import of PST files.."

$PSTFiles = Get-ChildItem -Path $path -Filter "*.pst"
$PSTFiles | Select-Object Name,Length,Directory | Export-CSV -path $filename -Encoding Unicode -NoTypeInformation

if ($archive -eq "Yes"){
WriteLog "Starting the import to mailbox archive.."
Dir $path\*.pst | Out-File $log -append

$PSTFiles | %{ New-MailboxImportRequest -Name ImportToArchive -BatchName ImportToArchive -Mailbox $_.BaseName -FilePath $_.FullName -IsArchive } | Out-File $log -append

}
Else{
WriteLog "Starting the import to normal mailbox.."
Dir $path\*.pst | Out-File $log -append

$PSTFiles | %{ New-MailboxImportRequest -Name ImportToMailbox -BatchName ImportToMailbox -Mailbox $_.BaseName -FilePath $_.FullName } | Out-File $log -append
}
WriteLog "PST files was imported successfully."

}

#Writes to log file
Function WriteLog ([string] $sLogMsg) {
$sLogLine=(get-date).Tostring("yyyy-MM-dd-HHmm")+","+$sLogMsg
$sLogLine| out-file $log -noclobber -append
}

#Script starts here
Import
WriteLog "Script completed!"

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show("Script completed!", "Information", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)

Feel free spread them just make sure to include a link to the blog as the source, use them at your own risk.

Download link