Import PSTs to Zimbra Mailboxes Powershell Script

This script I used to import a large amount of PST files to Zimbra mailboxes. The script builds the XML definitions file to be able to use the zcspstimportwizard application in batch mode and then runs the application against each file.

# ============================================================================
# 1. import pst mapping file
# 2. Get the primary STMP address
# 3. write definitions file
# Script must be ran in Exchange Powershell Console, otherwise,
# import Exchange modules
# Script calls for a CSV file named filelist.csv containing the column headers:
# file,samaccountname,userprincipalname
# ============================================================================
# set variables
$dc = “dc.local”
$mapfilename = “filelist.csv”
$mapfilepath = “c:\zimbra\zcspstimport\”
$mapfullfilepath = ($mapfilepath + $mapfilename)
$definitionsfolder = “definitions\”
$pstfolder = “F:\”
foreach ($i in (import-csv $mapfullfilepath))
# get email address info
# Uncomment next line if source is a mailuser
$mail = get-mailuser -identity $i.userprincipalname -domaincontroller $dc
# Uncomment next line if source is a mailbox
#mail – get-mailbox -identity $map.userprincipal
$definitionsfile = ($mapfilepath + $definitionsfolder + $i.samaccountname + “myfile.xml”)
Write-Host $definitionsfile
Write-Host ($pstfolder + $i.userprincipalname + “\” + $i.file)
#build xml file
$stream = new-object system.IO.StreamWriter($definitionsfile)
$stream.writeline(‘<Server name=”server”>’)
$stream.writeline(‘<Account name=”user”>’)
$stream.writeline(‘<userName>’ + $i.userprincipalname + “.archive” + ‘</userName>’)
$stream.writeline(‘<organizerEmail>’ + $mail.primarysmtpaddress + ‘</organizerEmail>’)
$stream.writeline(‘<Account name=”admin”>’)
$stream.writeline(‘<Data name=”psts”>’)
$stream.writeline(‘<PST>’ + $pstfolder + $i.userprincipalname + “\” + $i.file + ‘</PST>’)
$stream.writeline(‘<Options name=”config1″>’)
$stream.writeline(‘<Import Server=”server” TargetAccount=”user” AuthAccount=”admin” AdminPort=”7071″ AdminSSLEnabled=”1″ Data=”psts” Options=”config1″/>’)
$cmd = ‘C:\Zimbra\ZCSPSTImport-JB\ZCSPSTImportWizard-’
$param1 = ‘nopause’
#Run ZCSPSTImportWizard
&$cmd “$definitionsfile” “$param1”
wait-process ZCSPSTImportWizard- -timeout 3600


To append a file rather than a new file use syntax:

new-object system.IO.StreamWriter("filename.txt",1) or

new-object system.IO.StreamWriter("filename.txt",$false)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s