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

Tags: , , ,

8 Responses
Filed under:Uncategorized

8 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 :)

Leave a Reply