Quick Fix: Cancel Email Forwarding Exchange 2007/10

powershell2xa4Had 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.

About Robert Pearman
Robert Pearman is a UK based Small Business Server enthusiast. He has been working within the SMB IT Industry for what feels like forever. Robert likes Piña colada and taking walks in the rain, on occasion he also enjoys writing about Small Business Technology like Windows Server Essentials or more recently writing PowerShell Scripts. If you're in trouble, and you can find him, maybe you can ask him a question.

2 Responses to Quick Fix: Cancel Email Forwarding Exchange 2007/10

  1. 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.

Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: