While performing a migration to Office 365, I discovered that some mail enabled public folders had spaces in their Alias (AD attribute mailNickname). Unfortunately, Office 365 doesn’t support spaces in aliases, so I needed to correct that. Rather than do one at a time, I wrote a PowerShell script.
First, I needed to get the Active Directory objects in question. Fortunately, they have their own object class (publicfolder), so I could just filter on that. I also had to specify that I wanted to pull the mailNickname attribute. I knew I’d be using a foreach loop, so I pulled the AD objects into a variable:
$pfs = Get-ADObject -filter {objectclass -eq “publicfolder”} -Properties mailNickname
Then I needed to build the process I was going to loop through. I needed to get the mailNickname into a variable ( $nick = $pf.mailNickname ) and then I used the -replace parameter to pull all the spaces out ( $nospace = $nick -replace ” “, “” ).
Finally, I needed to make the change to the specific object. So, I decided to use the information I already had to identify the object to change ( Get-ADObject -Filter {mailNickname -eq $nick} ) and pipe it to Set-ADObject to make the change ( Set-ADObject -Replace @{mailNickname=$($nospace)} )
Here’s the entire script:
$pfs = Get-ADObject -filter {objectclass -eq “publicfolder”} -Properties mailNickname
foreach ($pf in $pfs){
$nick = $pf.mailNickname
$nospace = $nick -replace ” “, “”
Get-ADObject -Filter {mailNickname -eq $nick} | Set-ADObject -Replace @{mailNickname=$($nospace)}
}
Shane Skriletz, PEI
This didn’t work for me :
Get-ADObject -filter {objectclass -eq “publicfolder”}
This did:
Get-ADObject -Filter {ObjectClass -eq ‘publicFolder’}
Apparently it’s case-sensitive.