Exchange Management Shell Export Mailbox to PST script
Posted March 25th, 2009 by Matt ShadboltHere is s nifty script I chucked together that will prompt for a user name, configure the required permissions and then export the users mailbox to a PST file. All you need to do is change the $TargetArchiveDir location to the folder you wish to save the PST’s to. I’d suggest using a file share so the PST’s are saved in the same location no matter which admin exports the mailbox.
$TargetUserName = Read-Host “Enter the user you would like to Export”
$TargetArchiveDir = Get-Item “\\FILE-SERVER\ARCHIVE PST\”
$CurrentHostName = hostname
$CurrentProfile = Gwmi Win32_ComputerSystem -Comp $CurrentHostName
“Logged On As User: ” + $CurrentProfile.UserName
Write-Host “Setting Permissions” -ForeGroundColor Yellow
Add-MailboxPermission -Identity $TargetUserName -User $CurrentProfile.Username -AccessRights ‘FullAccess’
Write-Host “Exporting Mailbox” -ForeGroundColor Yellow
Export-Mailbox -identity $TargetuserName -PSTFolderPath $TargetArchiveDir -Confirm:$True
Write-Host “You can now Delete the user – I’m too scared to do it for you!” -ForeGroundColor Yellow
Tags: EMS, Exchange 2007, Export-Mailbox, PST
8 Responses

8 Responses to “Exchange Management Shell Export Mailbox to PST script”
April 30th, 2009 at 8:41 pm
Is this a powershell script?
May 1st, 2009 at 10:43 am
Yes – but you need to have the MS Exchange 07 snap-in installed in the shell.
May 12th, 2009 at 4:38 am
I modified this a little bit so that I could enter the user’s home path and have the file saved. It works but I am running into an issue where the unless I set the permission for full mailbox access manually in Exchange it will not run. When I run the script I get the error Add-mailboxpermission : Cannot bind argument to parameter ‘user’ because it is null. The script in fact still runs as long as I’ve added the mailbox permission already. Nice script though. Any thoughts on the error.
$TargetUserName = Read-Host “Enter the user you would like to Export”
$TargetArchiveDir = Read-Host “Enter the path to save the file to”
$CurrentHostName = hostname
$CurrentProfile = Gwmi Win32_ComputerSystem -Comp $CurrentHostName
“Logged On As User: ” + $CurrentProfile.UserName
Write-Host “Setting Permissions” -ForeGroundColor Yellow
Add-MailboxPermission -Identity $TargetUserName -User $CurrentProfile.Username -AccessRights ‘FullAccess’
Write-Host “Exporting Mailbox” -ForeGroundColor Yellow
Export-Mailbox -identity $TargetuserName -PSTFolderPath $TargetArchiveDir -Confirm:$True
Write-Host “You can now Delete the user – I’m too scared to do it for you!”
May 12th, 2009 at 10:28 am
I think you’ll need to do a Get-Item on the target directory first… so:
$TargetArchiveDir = Read-Host “Enter the path to save the file to”
$GetTargetArchiveDir = Get-Item $TargetArchiveDir
Export-Mailbox -identity $TargetuserName -PSTFolderPath $GetTargetArchiveDir -Confirm:$True
I find it strange that you’re getting permission errors before you hit the $TargetArchiveDir.
Are you sure your using a user account in the $CurrentProfile that has sufficient permissions? The $CurrentProfile retrieves your currently logged in username and password – I’d try running it with an Exchange Admin and also make sure you run the shell as Administrator. Let me know how you go.
May 13th, 2009 at 1:29 am
The Get-item did work.
I think the issue I have is that it fails when trying to get the current user and password. This is the error I get:
Get-WmiObject : Retrieving the COM class factory for component with CLSID {4590
F811-1D3A-11D0-891F-00AA004B2E24} failed due to the following error: 80040154.
At C:\scripts\exportmbx.ps1:5 char:23
+ $CurrentProfile = Gwmi <<<< Win32_ComputerSystem -Comp $CurrentHostName
What exchange permissions does the user running the script need to be able to add the full access right? I want to give the least permission as possible. Will recipient admin work?
May 13th, 2009 at 10:36 am
What happens when you run (line by line):
$CurrentHostName = hostname
$CurrentProfile = Gwmi Win32_ComputerSystem -Comp $CurrentHostName
“Logged On As User: ” + $CurrentProfile.UserName
When yo ucall $CurrentProfile.UserName you should receive the DOMAIN\Username your currently logged in with.
I haven’t tested different user permissions, however I don’t think recipient admin will work… I have a feeling you need to have Server Administrator rights or more.
One other thing – are you running Exchange 07 SP1? And are you running the EMS from XP or Vista?
May 14th, 2009 at 6:03 am
Well a couple things. I believe the issue may have been machine related. My xp machine was the issue as it runs fine on another xp machine and also a vista machine. I can in fact run this without having to use the Get-item cmdlet.
Thanks
May 15th, 2009 at 3:36 pm
Good stuff. It’s likely your .NET runtime version is old. Glad you’ve got it working though
Leave a Reply