Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active August 9, 2016 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bill-long/9d1cf8abb1faf8d39408 to your computer and use it in GitHub Desktop.
Save bill-long/9d1cf8abb1faf8d39408 to your computer and use it in GitHub Desktop.
$path = '\\server\path\'
$mailboxes = get-mailbox somemailboxfilter
$mailboxes | % {
$folders = Get-MailboxFolderStatistics $_.Alias -archive
$folders = $folders | ? {$_.Name -ne "Top of Information Store"}
$excludeFolders = $folders | % { $_.FolderPath.Substring(1) }
New-MailboxExportRequest -Mailbox $_.Alias -ExcludeFolders $excludeFolders -ExcludeDumpster -IsArchive -FilePath ($path + $_.Alias + '.pst')
}
@andrewsaraceni
Copy link

We (Wharton) ended up having difficulty with users who had forward slashes in their folder paths, as it appears as a different (unrecognized?) character type in the FolderPath and other properties, and thus doesn't get interpreted in total as a valid folder path in the exclude array.

My solution (based off of the link below) was to do a replace of the (proper) forward slash with a backslash, split the directory structure based on the backslash, replace the badly encoded character with a proper forward slash and escape it (with a backslash) per directory, and rejoin on the forward slash for a valid path.

$path = "\\network\path\"
$mailboxes = import-csv -path '\\network\path\testarchiveexport.csv'

foreach ($mailbox in $mailboxes)
{
    $folders = Get-MailboxFolderStatistics -Identity $mailbox.Identity -Archive
    $folders = $folders | Where-Object { $_.Name -ne "Top of Information Store" }
    $username = Get-Mailbox -Identity $mailbox.Identity

    [String[]]$excludeArray = $null
    foreach ($folder in $folders)
    {
        $slashReplace = $folder.FolderPath.Replace("/","\")
        $splitFolder = $slashReplace.Split("\")

        $dirTreeCount = $splitFolder.Count
        $newSplitFolder = $splitFolder[1..$dirTreeCount]

        $replacedFolder = $newSplitFolder | ForEach-Object { [String]$_.Replace([Char]63743,"/") }
        $replacedFolder = $replacedFolder.Replace("/", "\/")
        $excludeArray += [String]::Join("/", $replacedFolder)  
    }

    New-MailboxExportRequest -Mailbox $mailbox.Identity -ExcludeFolders $excludeArray -ExcludeDumpster -IsArchive -FilePath ($path + $username.Alias + ".pst")
}

https://social.technet.microsoft.com/Forums/en-US/a7706dc3-67a4-4a8d-99b0-3de879e9554b/question-marks-and-forwardslashes-in-outlook-folder-names-and-how-to-use-with?forum=winserverpowershell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment