Skip to content

How to Sync Exchange Online Proxy Addresses with Active Directory

Sync Exchange Online Proxy Addresses with Active Directory

Section titled “Sync Exchange Online Proxy Addresses with Active Directory”

This script connects to Exchange Online, retrieves all mailboxes, and matches each mailbox with a corresponding Active Directory (AD) user based on the primary SMTP address. It then filters out email addresses from a specified excluded domain and formats the remaining proxy addresses. These filtered proxy addresses are then added to the AD user. The script also includes error handling for cases where no or multiple matching AD users are found, and logs any issues with updating the AD user.


Ideal for: Exchange administrators managing hybrid email environments

Key Features:

  • Mailbox Synchronization - Connects Exchange Online with Active Directory
  • Domain Filtering - Excludes specified domains from proxy address sync
  • Error Handling - Comprehensive logging for troubleshooting
  • Batch Processing - Handles multiple mailboxes efficiently

Terminal window
# Define the Exchange Online organization and excluded domain
$Organization = "xxx.com"
$ExcludeDomain = "xxx.onmicrosoft.com"
# Connect to Exchange Online
Connect-ExchangeOnline -Organization $Organization -ErrorAction Stop
# Get all mailboxes
$AllMailboxs = Get-Mailbox
# Get all Active Directory users
$AllADUsers = Get-ADUser -Filter *
# Loop through each mailbox
foreach ($Mailbox in $AllMailboxs) {
    # Find the corresponding AD user based on the mailbox's primary SMTP address
    $ADUser = $AllADUsers | Where-Object EmailAddress -eq $($Mailbox.PrimarySmtpAddress)
    # Handle different scenarios based on the number of matching AD users
    switch ($ADUser.count) {
        0 { Write-Warning "No AD User found with $($Mailbox.PrimarySmtpAddress) E-mail address" }
        {$_ -gt 1} { Write-Warning "Multiple AD Users found with $($Mailbox.PrimarySmtpAddress) E-mail address" }
        1 {
            # Filter out addresses with excluded domain and correct formatting
            $Addresses = $Mailbox.EmailAddresses | Where-Object {($_ -like "smtp:*") -and ($_ -notlike "*$ExcludeDomain")}
            if ($Addresses) {
                # Correct formatting for proxy addresses
                $Addresses = $Addresses -creplace "SMTP:", "smtp:"
                # Prepare hash to add proxy addresses
                $HashToAdd = @{'proxyAddresses' = $Addresses}
                try {
                    # Update the AD user with new proxy addresses
                    $ADUser | Set-ADUser -Add $HashToAdd -ErrorAction Stop
                    Write-Output "Updated $($Mailbox.EmailAddresses) proxyAddresses successfully"
                }
                catch {
                    Write-Output "Failed to update $($Mailbox.EmailAddresses) proxyAddresses. Error: $($_.Exception.Message)"
                }
            }
        }
    }
}

ComponentRequirementPurpose
Exchange Online ModuleExchangeOnlineManagementConnect to Exchange Online
Active Directory ModuleActiveDirectoryManage AD user attributes
Exchange Admin RightsMailbox management permissionsAccess mailbox data
AD Admin RightsUser attribute modificationUpdate proxy addresses
ParameterExampleDescription
Organization"contoso.com"Exchange Online organization
ExcludeDomain"contoso.onmicrosoft.com"Domain to exclude from sync

  1. Connect to Exchange Online

    • Establish authenticated connection
    • Retrieve all mailbox data
  2. Retrieve Active Directory Users

    • Get all AD users with email attributes
    • Prepare for matching process
  3. Mailbox-AD User Matching

    • Match based on primary SMTP address
    • Handle multiple match scenarios
  4. Address Filtering and Formatting

    • Exclude specified domains
    • Format proxy addresses correctly
  5. Update Active Directory

    • Apply filtered proxy addresses
    • Log success/failure status

ScenarioResponseResolution
No AD User FoundWarning loggedManual user creation required
Multiple AD UsersWarning loggedReview duplicate accounts
Update FailureError loggedCheck permissions and data

Key Takeaway: This script provides automated synchronization between Exchange Online and Active Directory, ensuring consistent email address management while maintaining control over domain inclusion through configurable filtering.