Bulk Public Folder Rename and Cleanup Script

While doing a migration to Exchange 2010 a few years ago I ran into a situation where lots of the public folders would not replicate from 2007 due to the inclusion of characters in the name that exchange doesn’t like, but outlook allows, including (and most difficult to troubleshoot) a trailing space in the name.

unfortunately this client had over 15,000 individual public folders they had created over years of time with data they couldn’t loose, with folder names including shipment dates (and the “/” that went along with it) and many other things.

Additionally I added full permission to the administrator account since that had been stripped somehow over from many folders and was also causing a number of headaches.

This script ran through and removed all leading and trailing spaces, plus replaced forward and back slashes in the folder names with the pipe symbol.  It took a few days but everything finally replicated over without further trouble.

$zfold = get-publicfolder -recurse | where-object {$_.Name -like "*"}

foreach($zf in $zfold){
	$newName = $zf.name
	$newName = $newName.Trim()
	$newName = $newName.Replace('/', '|')
	$newName = $newName.Replace('\', '|')

	#write-host $newname
	set-publicFolder -Identity $zf.identity -Name $newName

	}

write-host "Permissions..."

$zfold = get-publicfolder -recurse | where-object {$_.Name -like "*"}

foreach($zf in $zfold){

	$Identity = $zf.ParentPath + "\" + $zf.name

	Add-PublicFolderClientPermission -Identity $Identity  -User "Administrator" -AccessRight Owner

	}
write-host "Done..."