So you made it past the first post, you are really wanting that double root canal… I mean, you really want to screw up your AD super bad. Well, you sir, are in luck! Because this is the first post where we are going to actually muck with your AD Partition.

Well, to be fair, this is particular script is pretty basic as far as what it edits, and it actually does have some error checking to make sure it doesn’t just write stuff willy nilly.

So, this script is designed to get all of the email addresses from your hosted accounts, and add them to the proxyAddresses space in on premise. When I first wrote it I kind of did it stupidly because it was carry over from another project. I’ve updated the way it handles setting up the proxyAddresses so that it doesn’t have to be a million lines. So, without further ado!

$mailboxes = Get-Mailbox -Resultsize Unlimited
$erroredUsers = @()
Set-Location AD:
foreach ($mailbox in $mailboxes) {
    $emails = $mailbox.EmailAddresses
    $userPrincipalName = $mailbox.userPrincipalName
    $path = $null
    $path = (Get-ADUser -Filter { userPrincipalName -like $userPrincipalName }).distinguishedName
    if ($path) {
        Set-ItemProperty -Path $path -Name proxyAddresses -Value $emails
    else {
        $erroredUsers += $mailbox.displayName
Set-Location c:
Write-Host -ForegroundColor Red "The following users had errors"
Write-Host -ForegroundColor Red $erroredUsers

If you hit any errors with this, please feel free to drop me a line and I’ll help out however I can.

This is a five part series!

Part 1
Part 2
Part 3
Part 4
Part 5

Leave a Reply

Your email address will not be published. Required fields are marked *