Exchange 2010

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

Tagged , , , ,