To make sure hidding mailboxes from Global Address List (GAL) works correctly you should verify few things
All the steps above I've done and shown that it's not working. The final and often omitted step is what also needs to be set for each user
Not so obvious right? Of course, if you have local Exchange, it would be done automatically, but most small Clients don't have resources to run an additional machine. While it's technically not a supported scenario, it's how most SMB Clients are working. Now we just need to fix all our users that have Mail field set, and are missing MailNickName.
$WhatIf = $true
$Forest = Get-ADForest
foreach ($Domain in $Forest.Domains) {
$Users = Get-ADUser -Filter { Mail -like '*' -and MailNickName -notlike '*' } -Properties mailNickName, mail,msExchHideFromAddressLists -Server $Domain
$Users | Format-Table -AutoSize Name, SamAccountName, DisplayName, Mail,mailNickName, Enabled, msExchHideFromAddressLists,DistinguishedName
foreach ($_ in $Users) {
Set-ADUser -Identity $_ -Replace @{mailNickname = $_.SamAccountName } -Server $Domain -WhatIf:$WhatIf
}
}