Archive for April 2013

Using PowerShell and ExMerge to Export Exchange 2003 Mailboxes to PST

To (bulk) export mailboxes from Exchange 2007 and higher versions you can use the Export-Mailbox command from PowerShell. Exchange 2003 utilizes Microsoft Exchange Server Mailbox Merge Wizard (ExMerge).

Starting ExMerge from PowerShell is possible through the Start-Process command. However, ExMerge requires a few specific parameters, including two files that define your servers and directories, and the mailboxes you want to export respectively.

In the PowerShell script below you can see a working example of how to incorporate ExMerge into PowerShell. After exporting the mailbox, the script renames the PST to <username>_backup.pst and moves it to the user's home directory but you're free to modify this behaviour.

This script depends on the free PowerShell Commands for Active Directory by Quest, which you can download here, and include by configuring your scripting environment, or you by adding the following line to the script:

Add-PSSnapin Quest.ActiveRoles.ADManagement 

Make sure ExMerge and the Exchange System Management Tools (if not run on the Exchange server) are installed and that your account has sufficient permissions to access the mailboxes (accounts must be enabled!). Configure the variables below "# Variables" to match your environment. Specify the user or users who's mailbox you'd like to export in the $usernames variable. Of course you could replace this with a clever query on Active Directory.

# 18-04-2013, D. Thijssen
# This script performs the following actions for one or multple users:
# 1. Export mailbox contents to .pst file
# 2. Move .pst file to user homedirectory
# Prerequisites:
# 1. ExMerge must be installed
# 2. Mailbox must be enabled and accessible

# Variables
$exchangeServer = "YOUR_EXCHANGE_SERVER"
$domainController = "YOUR_DOMAIN_CONTROLLER"
$dataDirectoryName = "C:\PowerShell\exportMailbox\"
$iniFile = "C:\PowerShell\exportMailbox\exmerge_batch.ini"
$fileContainingListOfMailboxes = "C:\PowerShell\exportMailbox\mailboxes_batch.txt"
$exMergeLocation = "C:\Program Files\Exchsrvr\bin\ExMerge.exe"

# Array of users to disable (comma-seperated strings containing usernames)
$usernames = "username1", "username2"
foreach ($username in $usernames) {
      if (Test-Path $iniFile) {             Remove-Item $iniFile       }       if (Test-Path $fileContainingListOfMailboxes) {             Remove-Item $fileContainingListOfMailboxes       }       $user = Get-QADUser -SamAccountName $username -IncludeAllProperties       $user.legacyExchangeDN | Out-File $fileContainingListOfMailboxes       "[EXMERGE]",       "SourceServerName=$exchangeServer",       "DomainControllerForSourceServer=$domainController",       "DataDirectoryName=$dataDirectoryName",       "FileContainingListOfMailboxes=$fileContainingListOfMailboxes" | Out-File $iniFile       $arguments = "-b", "-d", "-f $iniFile"       Start-Process -FilePath $exMergeLocation -ArgumentList $arguments -Wait       $source =  $dataDirectoryName + (($user.legacyExchangeDN).Substring(($user.legacyExchangeDN).LastIndexOf("cn=")+3)) + ".pst"       $target = ($user.HomeDirectory) + "\" + ($user.mailnickname) + "_backup.pst"       Move-Item -Path $source -Destination $target       if(Test-Path $source) {             Write-Host "User mailbox could not be exported."       } else {             Write-Host "User mailbox has been exported."       } }
Copyright Dave Thijssen. Powered by Blogger.