Welcome to Matts Admin tips. This blog is full of little fixes, tutorials and work-arounds to those anoying problems that a Systems Administrator might face. If you find something that you have found useful please leave me a comment - I appreciate knowing that I've helped! Also, feel free to leave a comment if my tips or ideas don't work in your environment... I monitor them closely and would be happy to help! Matt

Exchange Management Shell Export Mailbox to PST script

Here 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

Note: You need to run this script on a 32-bit machine with the Exchange Administration role installed

Posted by Matt Shadbolt
http://mattlog.net
Please comment or click an ad to support this site

Tags: , , ,

14 Responses
Filed under:Uncategorized

14 Responses to “Exchange Management Shell Export Mailbox to PST script”

  • Raymond Says:

    Is this a powershell script?

  • mattshadbolt Says:

    Yes – but you need to have the MS Exchange 07 snap-in installed in the shell.

  • Dennis Says:

    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!”

  • mattshadbolt Says:

    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.

  • Dennis Says:

    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?

  • mattshadbolt Says:

    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?

  • Dennis Says:

    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

  • mattshadbolt Says:

    Good stuff. It’s likely your .NET runtime version is old. Glad you’ve got it working though :)

  • Fernanda Says:

    When running the script, the prompt shows the following error:”The log file directory ‘C:\Program Files\Microsoft\Exchange Server\Logging\MigrationLogs’ does not exist.”

    Is there a solution for this?

  • Matt Shadbolt Says:

    Does it work if you create the directory?

  • Fernanda Says:

    I just create the directory and another error appeared:
    “Cannot open the log file ‘C:\Program Files\Microsoft\Exchange Server\Logging\MigrationLogs\export-Mailbox20100309-090258-6492356.log’.”
    I think this command seems to create a log file and open it but, when I come to the folder, the file is not there.

  • Matt Shadbolt Says:

    So it looks like the account your running the EMS shell from doesn’t have permissions to write to that directory. You need to run the shell as a user who has Exchange Admin group membership, and an account that has access to that logs directory?

    In the shell you are running EMS from, are you able to create a text file?

  • Fernanda Says:

    I ran the shell as administrator and the problem did not resolve.
    However, another error message appeared.
    I had to install the admin tools for exchange on my desktop because this command has to run on 32-bit computer.
    And now it runs ok.

    Thanks for the help.

  • Matt Shadbolt Says:

    Ah of course, I did know that the shell needs to be 32-bit but didn’t put that in my original post. Updated.

Leave a Reply