Dela via


Exchange 2007 and Export-Mailbox with the –IncludeFolders switch

You may have noticed that when you when you run the Export-Mailbox cmdlet, one of the switches available is –IncludeFolders.  With this switch, you can specify the folders to include in the export (duh!).  However, you may also notice that when you use the –IncludeFolders switch, it includes that folder and all subfolders.  This may not be desirable, and may lead to the export taking a long time to complete, especially if there are a large number of sub-folders.

If you don’t like this behavior, and are looking to export only certain folders (for example, just export the Inbox), I want to suggest a workaround.  You see, along with –IncludeFolders, there is another switch-ExcludeFolders.  Instead of specifying the list of folders to include, you specify the folders you want excluded.  Specifically, we tell the Export-Mailbox cmdlet that if it doesn’t match the “exact” path of the folder we want, it should be excluded.  Read on for more details.

First, we define a variable to do our “search”.  The reason why we have to do this is that we are first running Get-MailboxFolderStatistics, and the folderpath it outputs to is in the form of “/Folderpath”, but Export-Mailbox expects a folder path of “\FolderPath”, so we have to convert the forward slashes to backslashes.

$r = [regex]'/';

Next, we define another variable.  This is used to store our “list” of folders to exclude.  We use the Get-MailboxFolderStatistics to get a list of all folders that we want to “exclude”.  For example, where the folder path is not like the “Inbox”, and not like the “Top of Information Store”.  We have to include “Top of Information Store” here because the Inbox is a folder underneath the Top of Information Store, and if it isn’t included, the Inbox won’t be exported either.

From that output, we select only the FolderPath object (because that is all we need), we convert it to a String value (because right now, it is an Object), then change the “/” character to “\” and we store that as a variable. 

This can be modified to include other folders as well.  Just keep adding things to the where statement, for example $_.folderpath –ne “/Sent Items”.

$exclude = Get-Mailbox "Mailbox Name" | Get-MailboxFolderStatistics | where {$_.folderpath -ne "/" -and $_.FolderPath -ne "/Inbox"} | Select-Object FolderPath | Foreach {$_.folderpath.tostring()} | foreach {$r.replace($_”,”\”,-1)}

The -1 at the end tells it to iterate through all folders in the list.

OK – admittedly, that is a lot of work to get what you need, but it should work.  So now, you have a variable that contains an array of all the folders you want to “Exclude”.  You are ready to run the Export.

This will perform the export for the 1 user you define.  You can also modify the above to run against all users. 

Export-Mailbox "Mailbox Name" -FoldersExclude $exclude -TargetMailbox "Mailbox Name" -TargetFolder "Folder Name"

In the below example, it is running against all users except the mailbox you are exporting to, and getting a list of all folders except the Inbox, Top of Information Store, and Deleted Items.  Since the –ExcludeFolders doesn’t care if a folder doesn’t exist in a mailbox, we’ll keep the size of the list to a manageable level by only including Unique folder names.

$exclude = Get-Mailbox | where {$_.name –ne “Mailbox Exporting To”} | Get-MailboxFolderStatistics | where {$_.folderpath -ne "/" -and $_.FolderPath -ne "/Inbox" –and $_.FolderPath –ne “/Deleted Items”} | Select-Object FolderPath | Foreach {$_.folderpath.tostring()} | foreach {$r.replace($_”,”\”,-1)} | Sort -Unique

Now we are going to get the list of mailboxes to export.

Get-Mailbox | where {$_.name –ne “Mailbox Exporting to”} | Export-Mailbox –ExcludeFolders $exclude –TargetMailbox “Mailbox Exporting To” –TargetFolder “Folder name”

Now since I don’t have a large lab with lots of mailboxes that have lots of data (and folders) in them, I haven’t verified how much time this method will save, but I believe the time savings here will be substantial.  If you try this method out, please let me know if it saves you time.

Comments

  • Anonymous
    January 01, 2003
    Thanks David - I'll make the appropriate changes.

  • Anonymous
    July 26, 2010
    thks a lot for your suggestions

  • Anonymous
    August 13, 2010
    I think that these are great suggestions, will have to try it out to see if it works the way it should!

  • Anonymous
    September 25, 2010
    Hi.. Thanx a lot for the nice article above. I have an inquiry if u dnt mind; how export all the messages from sent from a particular user to a mailbox. e.g creating a pst file based on all the message sent from mailbox1@xyz.com to mailbox2@zyx.com?

  • Anonymous
    January 28, 2011
    You have some typos in your commands: $exclude = Get-Mailbox "Mailbox Name" | Get-MailboxFolderStatistics | where {$.folderpath -ne "/" -and $.FolderPath -ne "/Inbox"} | Select-Object FolderPath | Foreach {$.folderpath.tostring()} | foreach {$r.replace($”,””,-1)} should be (remove one of the " characters at the end): $exclude = Get-Mailbox "Mailbox Name" | Get-MailboxFolderStatistics | where {$.folderpath -ne "/" -and $.FolderPath -ne "/Inbox"} | Select-Object FolderPath | Foreach {$.folderpath.tostring()} | foreach {$r.replace($,””,-1)} and Export-Mailbox "Mailbox Name" -FoldersExclude $exclude -TargetMailbox "Mailbox Name" -TargetFolder "Folder Name" should be (the parameter is ExcludeFolders not the otherway around): Export-Mailbox "Mailbox Name" -ExcludeFolders $exclude -TargetMailbox "Mailbox Name" -TargetFolder "Folder Name"

  • Anonymous
    October 31, 2013
    this is a great article, well done.

  • Anonymous
    December 15, 2013
    Pingback from musther.com » Exchange 2010 – Managing Emails for Users