The company who I work for are currently in the process of migrating away from Exchange on premise and over to Office 365. Now, 95% of our ActiveDirectory records are set to inherit the mail recipient policy – the other 5% have been excluded for one reason or another.. As the technical lead on the project, I had to come up with a way to add the new/required email address to all mail accounts prior to migration. Which is there this little PowerShell script was born.. I must admit, I have somewhat of a love/hate relationship with PowerShell, I understand its powerful, and that things have to change.. Its just taking me sometime to get used to it.. Anyway, this is what I have so far, it may well be that it could be tweaked somewhat, so if you have any suggestions please do let me know!
Steps involved
- Search Exchange for all mailboxes with the inherit flag unticked
- Build the new email address using Firstname.Lastname
- If the first name and last name fields are blank in AD, build the address using the display name.
- Strip any spaces out of the name, and replace with full stops
- Finally – add the new mail address to the Exchange Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Write-Host "**** Office 365 Remediation Script v1.0 ****" Write-Host "Please wait - Filtering results for all mailboxes with policy inheritence disabled" $Mailboxes = Get-Mailbox -results unlimited | where { $_.EmailAddressPolicyEnabled -eq $False } Write-Host "Mailbox filter located " $Mailboxes.count " mailboxes for remediation" $i = 0 foreach ($Mailbox in $Mailboxes) { $i++ $User = Get-User $Mailbox.Alias Write-Host "Remediating " $User.Name " account" $O365Alias = $User.FirstName + "." + $User.LastName + "@mytennant.mail.onmicrosoft.com" $O365Alias -replace '\s','.' If ($O365Alias -eq ".@mytennant.mail.onmicrosoft.com" ){ Write-Host "!*!*!*!*!*!*!*!*!*!*! Alias not valid, reworking from User Full Name !*!*!*!*!*!*!*!*!*!*!" $O365Alias = $User.Name + "@ mytennant.mail.onmicrosoft.com" $O365Alias -replace '\s','.' } Write-Host "Setting mail alias as" $O365Alias set-mailbox -Identity $User.Name -EmailAddresses @{add=($O365Alias)} Write-Progress -activity "Remediating Mailboxes" -status "Percent added: " -PercentComplete (($i / $Mailboxes.count) * 100) } Write-Host "Remediation work complete" |