r/PowerShell • u/FastFredNL • Oct 22 '26
Question Exchange (Online) Powershell auto-forward and auto-reply
Ok, I need some help with Powershell.
Part of our company got sold. This weekend, the migration is set to go through. All users that are part of this transfer will login into the new owners IT infrastructure and their 365 tenant starting next monday. The mailbox contents will be synced and for the next few weeks all incoming e-mail needs to be auto-forwarded to the user's new mailbox in the other tenant. And a auto-reply will be sent to the sender informing them of the upcoming discontinuation of the old e-mail address.
So, all e-mail coming in to 'olduser' needs to be forwarded to 'newuser' and newuser is in another tenant. To prevent double e-mail, 'olduser' should not be seeing the e-mail in their inbox.
Currently this is what I have, being run from Exchange Management shell connected to Exchange Online
$Users = Import-Csv "c:\temp\usermailboxentest.csv" -delimiter ";"
foreach ($User in $Users) {
Set-Mailbox -Identity $User.adresold `
-ForwardingSmtpAddress $User.adresnew `
-DeliverToMailboxAndForward $false
}And then, I run this
$CsvPath = "C:\Temp\mailboxes.csv"
$StartTime = Get-Date
$EndTime = [datetime]::new(2026, 12, 30, 23, 59, 59)
$Results = foreach ($CsvUser in Import-Csv -Path $CsvPath) {
$MailboxAddress = $CsvUser.MailboxAddress.Trim()
try {
Write-Host "Processing: $MailboxAddress" -ForegroundColor Cyan
# Retrieve the mailbox
$Mailbox = Get-Mailbox -Identity $MailboxAddress -ErrorAction Stop
# Determine the configured forwarding address
if ($Mailbox.ForwardingSmtpAddress) {
# Forwarding to an SMTP address
$ForwardAddress = $Mailbox.ForwardingSmtpAddress.ToString() `
-replace '(?i)^smtp:', ''
}
elseif ($Mailbox.ForwardingAddress) {
# Forwarding to an internal Exchange recipient
$ForwardRecipient = Get-Recipient `
-Identity $Mailbox.ForwardingAddress `
-ErrorAction Stop
$ForwardAddress = $ForwardRecipient.PrimarySmtpAddress.ToString()
}
else {
throw "No mailbox-level forwarding address is configured."
}
Write-Host "Forwarding address: $ForwardAddress" -ForegroundColor Green
# Create the HTML automatic-reply message
$AutoReplyMessage = @"
<p><strong>Dear Sender,</strong></p>
<p>
This email address has been discontinued etc. etc etc. and a variable for $ForwardAddress pointing the user to whatever the new e-mail address is.
</p>
"@
# Configure automatic replies for internal and external senders
Set-MailboxAutoReplyConfiguration `
-Identity $MailboxAddress `
-AutoReplyState Scheduled `
-StartTime $StartTime `
-EndTime $EndTime `
-InternalMessage $AutoReplyMessage `
-ExternalMessage $AutoReplyMessage `
-ExternalAudience All `
-ErrorAction Stop
Write-Host "Automatic reply configured successfully." -ForegroundColor Green
[PSCustomObject]@{
MailboxAddress = $MailboxAddress
ForwardAddress = $ForwardAddress
Status = "Success"
Error = ""
}
}
catch {
Write-Warning "Failed for $MailboxAddress`: $($_.Exception.Message)"
[PSCustomObject]@{
MailboxAddress = $MailboxAddress
ForwardAddress = $null
Status = "Failed"
Error = $_.Exception.Message
}
}
}
# Show the results
$Results | Format-Table -AutoSize
# Export a processing report
$Results | Export-Csv `
-Path "C:\Temp\AutoReplyResults.csv" `
-NoTypeInformation `
-Encoding UTF8Now this is what happens, when I set DeliverToMailboxAndForward to $true, the autoreply works but the original e-mail is put into the old mailbox which I don't want to happen because in the new tenant users will end up with double e-mail because of the sync of mailbox contents.
When I set that to $false however, the autoreply doesn't work because 'olduser' mailbox is never 'hit'
Also, this will be for about 170 mailboxes, both user and shared boxes.
2
u/schnitzeljaeger Oct 22 '26
Afaik this won't work with what MS gives you... Do you have a signature tool like the one from CodeTwo? With that one you could set up autoresponders sitting in front of the EOL infrastructure.
2
u/BlackV Oct 22 '26
you should probably have a look too and removing those back ticks
See here: Bye Bye Backtick: Natural Line Continuations in PowerShell for some good information
2
u/Net-Runner Oct 23 '26
ForwardingSmtpAddress to an external domain gets blocked by the outbound spam policy unless auto-forwarding is set to On. Check that first, or the forwards will silently fail.
1
u/fatalicus Oct 22 '26
What we did when migrating 70k or so users to new tenants is that we did a full migration, then incremental migrations once a day.
We then set an end date, and when that date came we stopped migrations and enabled forwarding and auto replies.
Realy only good way we found to avoid double emails in the new mailbox.
1
u/capitolgood4 Oct 22 '26
This solution should work, and while you could build the automation in your tenant... why should you? Instead, you can put the configuration obligation onto the new tenant since it's now their customers that need the updated address information :D
Create a transport rule targetting the security group and have it add a BCC to the autoreply mailbox in the new tenant.
Then in the new tenant, they would need to:
1
u/ashimbo Oct 24 '26
Stop forwarding, and let the sync handle it.
0
u/BlackV Oct 22 '26
ouch, why not add that domain as an alias/mx on the new tennant ?