Fixing Leading and Trailing Whitespace in Exchange Objects

Thursday, March 25, 2010
You must remove all the leading and trailing spaces from Exchange 2000/2003 user names, group names, and Public Folder names prior to migrating them to Exchange 2007 or Exchange 2010.  Previous versions of Exchange and Outlook would let you create these objects, usually by accident.  Exchange 2007 and Exchange 2010 have strict conformance rules that will not allow this, so you'll have to fix it before the objects are migrated.

For example, you may use the Exchange AddReplicaToPFRecursive.ps1 script to add your Exchange 2007 or 2010 servers as replicas to your Public Folders.  If the Public Folder contains leading or trailing whitespace you will receive an error in the Exchange Management Shell (EMS):

Set-PublicFolder : The Name property contains leading or trailing whitespace, which must be removed.
At C:\Program Files\Microsoft\Exchange Server\Scripts\AddReplicaToPFRecursive.ps1:147 char:24
+ $_ | Set-PublicFolder <<<< -server $_.OriginatingServer;
WARNING: Object \Information Technology\Website Resources\Software Development has been corrupted and it is in an inconsistent state. The following validation errors have occurred:
WARNING: The Name property contains leading or trailing whitespace, which must be removed.

Use the following PowerShell one-liners to trim the leading and trailing whitespace from Exchange objects in AD:


Single User Object:
Get-Mailbox -Identity USER | Foreach { Set-Mailbox -Identity $_.Identity -DisplayName $_.DisplayName.Trim() }

All User Objects:
Get-Mailbox | Foreach { Set-Mailbox -Identity $_.Identity -DisplayName $_.DisplayName.Trim() }


Single Public Folder:
Get-PublicFolder -Identity "\Test\SubPath\PublicFolderName" | Set-PublicFolder -Identity $_.Identity -Name $_.Name.Trim()

All Public Folders:
Get-PublicFolder -Identity "\" -Recurse -ResultSize Unlimited | Foreach { Set-PublicFolder -Identity $_.Identity -Name $_.Name.Trim() }


Single Distribution Group:
Get-DistributionGroup -Identity GroupName | Set-DistributionGroup -Identity $_.Identity -DisplayName $_.DisplayName.Trim()

All Distribution Groups:
Get-DistributionGroup | Foreach { Set-DistributionGroup -Identity $_.Identity -DisplayName $_.DisplayName.Trim() }

Note: These commands all give a warning if the object is not changed, which you can safely ignore:

WARNING: The command completed successfully but no settings of '\xxxxx\xxxxxx\xxxxx\IT Department Calendar' have been modified.

Another configuration that can cause errors is when a Public Folder alias contains spaces. Use the following one-liners to remove spaces from Public Folder aliases:

Remove Spaces From a Single Public Folder Alias:
Get-PublicFolder -Identity "\Test\SubPath\PublicFolderName" | Get-MailPublicFolder |
Where {$_.Alias -like "* *"} | ForEach-Object { Set-MailPublicFolder $_.identity -Alias:($_.Alias -Replace " ","") }

Remove Spaces From All Public Folder Aliases:
Get-PublicFolder -Identity "\" -Recurse -ResultSize Unlimited | Get-MailPublicFolder |
Where {$_.Alias -like "* *"} | ForEach-Object { Set-MailPublicFolder $_.identity -Alias:($_.Alias -Replace " ","") }

These PowerShell one-liners demonstrate how easily some tasks can be performed.  This might take days to do using the GUI with thousands of objects.


  1. These are really easy to use PS one-liners which make the life a lot easier. Thanks

  2. Hi,
    "Remove Spaces From a Single Public Folder Alias:" works just fine :-)

    "Remove Spaces From All Public Folder Aliases:" comes with the error seen below :-(

    Any clue on whats going wrong?
    I have many many PF so it would be great to get the "All" script to work.

    Im running on Exchange 2010 sp1.

    BR & Thanks,


    Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [],
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

  3. I get the exact same pipeline error when trying to fix all folders. Can anyone shed some light?

  4. I know it's an old question:

    The pipeline error occurs because this particular command cannot be processed in a pipeline.

    For the all users example, you would have to instead do:

    $a = Get-Mailbox -ResultSize Unlimited
    $a | Foreach { Set-Mailbox -Identity $_.Identity -DisplayName $_.DisplayName.Trim() }

  5. Avi I tried your example and it does't work, as 'a$' doesn't include the the Public Folders with Warnings

    $a = Get-PublicFolder -Identity "\" -Recurse -ResultSize Unlimited | Get-MailPublicFolder | Where {$_.Alias -like "* *"}
    $a | ForEach-Object {Set-MailPublicFolder $_.identity -Alias:($_.Alias -Replace " ","")}

    Any other clues??

  6. This is brilliant - exactly what I was searching for. Thanks for sharing it, I'm sure you're not the only one I've helped.

  7. Hi,

    This is great - Remove Spaces From All Public Folder Aliases:

    But is there a way to only get it look at mail-enabled public folders so it doesnt recurse every single public folder in the tree?


Thank you for your comment! It is my hope that you find the information here useful. Let others know if this post helped you out, or if you have a comment or further information.