Exchange 2007 Powershell Scheduling Export to PST Script

Britt ThompsonSr. Systems Engineer
Published:

Requirements

Exchange 2007 Server
32-bit operating system - *The Mailbox-Export command will not run on a 64-bit OS
Exchange 2007 Management tools - Install Exchange Management Tools
Windows Powershell
Outlook 2003 or 2007 installed on the machine running the script
A user (not the built-in Administrator) that is at least Exchange Organization Admin or an Exchange Server Admin

Creating a User to Export With

I create a super user by copying the built-in administrator account in AD. This gives me the privileges I need and Exchange deny privileges don't carry over.
Open the Exchange Management Shell and use the following command replacing the name of the mailbox database (this is the default) and your newly created username
Add-ADPermission -Identity "Mailbox Database" -User "username" -ExtendedRights Receive-As

Open in new window

After the command runs successfully this is the user account you'll need to be logged in with in order to run the scheduled pst backups. This is the same username you schedule the task to run as.

The Batch File

The batch file portion of this is the object that's scheduled to run at the time you choose. This batch file does nothing but call a Powershell command script (these can not be scheduled directly so the .bat file is required).

Batch File Creation

Create a text document and save it as pstexport.bat
Copy the following line into your text file replacing the file location within the curly brackets
Powershell -command "& { \\server\share\scripts\exch\pstexport.ps1 }"

Open in new window

(We'll create the pstexport.ps1 Powershell script in the next section)
Save the .bat file to the same location where the pstexport.ps1 will be saved (this does not have to be a server share)

The PS1 File

A PS1 file is a standard Powershell script file, like a shell script or vb script. This standard Powershell script will need to load the Exchange CmdLets included in the Exchange Management Shell. Once the CmdLets are loaded it will create a log file with the date and an extension.

PS1 File Creation

Create a text file and name it pstexport.ps1 and save it to the location you chose above.
Add the Exchange Powershell snap-in to include the CmdLets. Copy and paste the following into your ps1 file
Adding Exchange Snap In to execute Exchange CmdLets in this script
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

Open in new window

Now, we need to create the log file variables that give the log files a unique name. Copy and paste the following below the previous section
#Log File Name Variables
                      $filedate=get-date -f "yy-MM-dd"
                      $fileext="_exchbak.txt"

Open in new window

Last, we add the shell script that runs the Export-Mailbox command. In this case I export the mailboxes in a specific OU in Active Directory. Make the necessary adjustments to fit your exportation needs
#Exchange Shell Command to Export Mailboxes to PST
                      Get-Mailbox -OrganizationalUnit "Company Users" | Export-Mailbox -PSTFolderPath \\server\archive\pst -Confirm:$false > \\server\share\scripts\exch\logs\$filedate$fileext
                      exit

Open in new window

You'll need to create the "logs" directory within the scripts directory to allow the logs to be created.

Closing

What you should have now is a share at \\server\share\scripts\exch that contains 2 files and 1 folder. The pstexport.bat, pstexport.ps1 and a logs directory. All you need to do is set the schedule to run your .bat file at whatever time you choose.

*NOTES
Remeber you need a 32-bit OS to run this command in the PowerShell and I recommend scheduling with Vista or XP unless you feel like fighting with privileges on the script when it tries to run. I setup a virtual machine just run this script
If you're doing this at a company with massive mailboxes you may need to extend the default PST size limit of 20GB in Outlook 2007. There's a simple registry change (I've extended it to 70GB in this example...yes, 70GB!)
Windows Registry Editor Version 5.00
                      
                      [HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\PST]
                      "WarnLargeFileSize"=dword:00011400
                      "MaxLargeFileSize"=dword:00011800

Open in new window

This script incrementally appends the email to the PST files after the initial export so the PST files gradually get larger and you may want to periodically delete or archive the PST files.
0
8,292 Views
Britt ThompsonSr. Systems Engineer

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.