Script repository

Update suffix of the primary SMTP address

Updated on: Jan 18, 2026, Views: 2344

Exchange

The script forms a new primary SMTP address based on the prefix of the current primary address and the domain part specified in a parameter. To execute the script, create a custom command with a text parameter that will be used to specify the domain part for the new primary SMTP address.

In the script, the $suffixParameterName variable specifies the name of the parameter take the new primary SMTP address suffix from. The name should start with the param- prefix.

$suffixParameterName = "param-MyParam" # TODO: modify me

# Get parameter value.
$suffix = $Context.GetParameterValue($suffixParameterName)

# Get primary SMTP Address.
$mailboxParams = $Context.TargetObject.GetMailParameters()
$emailAddresses = $mailboxParams.EmailAddresses

for ($i = 0; $i -lt $emailAddresses.Count; $i++)
{
    $emailAddress = $emailAddresses.GetAddress($i,[ref]"ADS_PROPERTY_NONE")
    if ($emailAddress.IsPrimary -and $emailAddress.Prefix -eq "smtp")
    {
        $primarySMTPAddress = $emailAddress.Address
        break
    }
}

$addressPrefix = $primarySMTPAddress.Substring(0, $primarySMTPAddress.IndexOf("@"))

# Create an instance of the 'AdmExchangeMailboxParameters' class.
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"

# Disable automatic update of e-mail addresses based on e-mail address policy.
$mailboxParams.EmailAddressPolicyEnabled = $False

$emailAddresses = $mailboxParams.EmailAddresses
$emailAddresses.OverrideOldValues = $False

# Create a new email address.
$emailAddress = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddress.Address = "$addressPrefix@$suffix"
$emailAddress.IsPrimary = $True

# Add the new email address to the existing list.
$emailAddresses.Add("ADS_PROPERTY_APPEND", $emailAddress)
$mailboxParams.EmailAddresses = $emailAddresses

# Save the changes
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.