Quick Fix: Cancel Email Forwarding Exchange 2007/10
October 24, 2012 2 Comments
Had a call from a client who needed to cancel all automatic forwarding of email to one recipient.
Easy you might think.
This server has over 100 mailboxes on it and 3 Exchange Servers, and without knowing whose email is actually set to forward to the mailbox in question, it could be quite time consuming to find each individual user and change their properties.
Enter, PowerShell.
Opening the Exchange Shell i entered the following:
Get-Mailbox | Where-Object { $_.ForwardingAddress –eq “domain.local/OU/User” }
This command will search the entire organisation for any mailbox who is set to forward their email to the specefied user.
For example, if it were my account that email was being forwarded to..
Get-Mailbox | Where-Object { $_.ForwardingAddress –eq “sbs.local/SBS Users/Robert Pearman” }
This gave me an output showing every user that has mail set to auto forward to the mailbox in question.
Because there were many results, i decided to go one step further.
$users = (Get-Mailbox | Where-Object { $_.ForwardingAddress –eq “domain.local/OU/User” })
ForEach ($User in $users) { Set-Mailbox –Identity $user –DeliverToMailboxAndForward $False –ForwardingAddress $null}
Using the ‘ForEach’ command i can take the output from my first command ($users) and then run a follow up command on each resulting user.
This takes care of the whole job in about 30 seconds.
I think this is a little complicated.
Hi Kathleen,
Understood, that it perhaps appears a little more complex than simply going through the GUI interface, however, when you compare the two methods, and review the PowerShell code, you will see that it really is not that difficult to follow.
(Get-Mailbox | Where-Object { $_.ForwardingAddress –eq “domain.local/OU/User” })
This command will return a list of mailboxes that match the criteria. In this instance, the criteria is, any mailbox who is forwarding to X user.
$users = (Get-Mailbox | Where-Object { $_.ForwardingAddress –eq “domain.local/OU/User” })
Adding the ‘ $Users = ‘ in front of the command, stores those mailboxes as a variable.
We then simple run another command,
Set-Mailbox –Identity $user –DeliverToMailboxAndForward $False –ForwardingAddress $null
Which would cancel mail forwarding.
We could amend it like this..
Set-Mailbox –Identity RobertPearman –DeliverToMailboxAndForward $False –ForwardingAddress $null
This would find my mailbox, and cancel any email forwarding i had configured.
Using the ForEach command, allows us to process each mailbox stored in $users, in turn, and makes the whole process more effecient.
ForEach ($User in $users) { Set-Mailbox –Identity $user –DeliverToMailboxAndForward $False –ForwardingAddress $null}
In the example above, each object stored inside $users is used one by one, inside the variable of $user.
So if $users returned,
UserA
UserB
UserC
Instead of us having to run 3 Set-Mailbox commands, we simply run one command.
ForEach ($user in $users)
So as each object is read from $users.. User A becomes $user, when the Set-Mailbox command has run, $user becomes UserB and so on, until each of the objects has been processed.
Hope this helps.