tips_and_howtos:create_and_manage_office_365_mailusers_with_powershell

Manage Office 365 mailusers with multiple proxyAddresses using Microsoft Powershell

For some reason the Microsoft (and other) documents are restrained to situations where mailuser does not have multiple email addresses which in my case is not realistic. Somehow this reminds me of Jim Carrey karate instructor lessons. :)

Anyway I wrote down here how to enable, manage and disable Office 365 mailusers with multiple proxyAddresses using Powershell command line tools for automatic scripting. Also included is existing mailuser checking and try - catch method for error handling.

The below stuff is tested on live environment and should work but please let me know if there is something that I should correct.

For completeness sake here is the common things you need to do before you can talk with Exchange / Office 365

$url = "http://servername/powershell"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -Authentication Kerberos
Import-PSSession $Session

In addition to that you may need to set up variables for mail and proxyAddresses.

We must know if we want to Enable-MailUser or Set-MailUser. Please note that Get-MailUser throws an error if user is not found so you must use try-catch.

try {
    mbx = Get-MailUser -Identity $principalName
}
catch { 
    $errorMessage = "Get-Mailuser failed for user $principalName. Exception type: "  + $_.Exception.GetType().FullName + ", message: " + $_.Exception.Message
    <return errormessage to the calling entity>
}

If we have an AD user but no existing mail account we can do the following:

if (-not $mbx.Name) {
    try {
        Enable-MailUser -Identity $principalName -Alias $mail.Split('@')[0] -ExternalEmailAddress $address
    }
    catch {
        $errorMessage = "Error in Enable-Mailuser operation for new mailuser $principalName. Exception type: "  + $_.Exception.GetType().FullName + ", message: " + $_.Exception.Message
    }
}

Add the multiple addresses that are contained in an array $proxyAddresses:

try {
    foreach ($address in $proxyAddresses) { 
        Set-MailUser -Identity $principalName -EmailAddresses @{Add=$address}
    }
}
catch {
    $errorMessage = "Error in Set-Mailuser for existing mailuser $principalName. Exception type: "  + $_.Exception.GetType().FullName + ", message: " + $_.Exception.Message
}

Now we only need update the values to the mailuser using Set-Mailuser.

if ($mbx.Name) {
    try {
        foreach ($address in $proxyAddresses) { 
            Set-MailUser -Identity $principalName -Alias $mail.Split('@')[0] -EmailAddresses @{Add= $address} -ExternalEmailAddress $mail
    }
    catch {
        $errorMessage = "Set-Mailuser error for user $principalName. Exception type: "  + $_.Exception.GetType().FullName + ", message: " + $_.Exception.Message
    }
}

In the end we wish to terminate session.

Remove-PSSession $Session

And that's about it.

Comments and suggestions

If you find bugs above please comment below. Also feel free to rate.

{(rater>id=createandmanageoffice365mailuserswith_powershell|type=rate|trace=ip|tracedetails=0|headline=off)}

  • tips_and_howtos/create_and_manage_office_365_mailusers_with_powershell.txt
  • Last modified: 2021/10/24 13:46
  • (external edit)