Script repository

Disable mail forwarding if forwarding address matches regular expression

Updated on: Jan 18, 2026, Views: 4237

Exchange

The script disables the mail forwarding feature for the target Exchange mailbox if the forwarding address matches a regular expression. To execute the script, create a business rule, custom command or scheduled task configured for the required object type.

In the script, the $regex variable specifies the regular expression to use.

$regex = "\w+@abc\.com" # TODO: modify me

# Get forwarding recipient.
try
{
    $altRecipientDN = $Context.TargetObject.Get("altRecipient")
}
catch
{
    return
}

# Check the recipient's email address against the regular expression.
$object = $Context.BindToObjectByDN($altRecipientDN)
$mail = $object.Get("mail")

if ($mail -notmatch $regex)
{
    return
}

# Disable mail forwarding.
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"

$deliveryOptions = $mailboxParams.MailFlowSettings.DeliveryOptions
$deliveryOptions.IsModificationEnabled = $True

$deliveryOptions.ForwardingAddressModificationEnabled = $True

# Save 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.