update all exchange mailboxes to forward to external mail

Hi - I have roughly 500 users in my organization on Exchange 2010.  We are going to Google Apps (K12 environment) and I would like to forward incoming all mail from the Exchange server to our new Google Domain.  My user names are all the same, (e.g. bob.smith@ourexchange.domain = bob.smith@ourgoogle.domain).

Rather than hope my users could write their own rule to forward their mail from the exchange address to the google address, and rather than access each of 500 mailboxes one at a time to forward the email, can somebody help me with a script or the command line(s) to use the Exchange Shell to add a forwarding rule to all of my users mailboxes?  If I could limit it by OU, then I could test/troubleshoot and then phase in the mass-forwarding, but that is not a requirement if it is not an option.

I am thinking that just adding the forward to the google domain and also deliver the message to the original Exchange mailbox would be best, rather than just shoving them all to the Google Account...

Any help the experts can provide is appreciated!
Stephen YorkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joshua GrantomSenior Systems AdministratorCommented:
Something like this should work.

# Loop though the object returned by Get-Mailbox with each element represented by $mailbox

foreach ($mailbox in (Get-MailBox -ResultSize Unlimited  -OrganizationalUnit yourdomain/Staff)


# Create the forwarding address string
$ForwardingAddress=  $mailbox.Name + “.” + $mailbox.sn + "@ourgoogle.domain"

# Check for contact, if not add one

If (!(Get-MailContact $ForwardingAddress -ErrorAction SilentlyContinue))


New-MailContact $ForwardingAddress-ExternalEmailAddress $ForwardingAddress- OrganizationalUnit yourdomain/GoogleContacts | Set-MailContact -HiddenFromAddressListsEnabled $true

# Set the forwarding address
Set-Mailbox $mailbox -ForwardingAddress $ForwardingAddress -DeliverToMailboxAndForward:$true 


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
your exchange.domain should equal your google.domain once you change the MX records to point to googlemail.com
Murali ReddyExchange ExpertCommented:
Here you go, you need not even create any contact objects, unless you have other requirements.

$mailbox= Get-Mailbox -ResultSize "Unlimited"
foreach ($i in $mailbox)
[string]$forwardingaddress = ($i.primarysmtpaddress); $forwardingaddress= $forwardingaddress.Replace("ourexchange.domain", "ourgoogle.domain")
Get-Mailbox $i.primarysmtpaddress |Set-Mailbox -ForwardingAddress $forwardingaddress -DeliverToMailboxAndForward $true

##replace the smtp suffixes accordingly
###I assumed your statements about the smtp addresses in the post. i.e. "My user names are all the same, (e.g. bob.smith@ourexchange.domain = bob.smith@ourgoogle.domain)"
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Joshua GrantomSenior Systems AdministratorCommented:

He wanted the ability to specify an OU so he could test, I included creating contacts so he could see in EMC which users actually had a forward successfully applied.
Murali ReddyExchange ExpertCommented:
Thanks Joshu, I missed that. Just a string to add in the first line

$mailbox= Get-Mailbox -ResultSize "Unlimited" -OrganizationalUnit "OU path"


to check what users are applied, run

Get-Mailbox -resultsize "unlimted" |select name,primarysmtpaddress,ForwardingAddress, DeliverToMailboxAndForward
Stephen YorkAuthor Commented:
Our domain name is changing so the MX record change is not an option.  

The script is most cool.  Let me try this out.  Thank you!
Stephen YorkAuthor Commented:
Hmmm, I'm striking out here, but I am sure it is a typo on my part.I created a test OU and placed 2 users in it with mailboxes (etest1 & etest2).  My current domain is ravenna.portage.k12.oh.us, my Google domain is: ravennaschools.us.

My script looks like this for my test OU:

$mailbox= Get-Mailbox -ResultSize "Unlimited" -OrganizationalUnit "OU=Test,ou=BOE,OU=Staff,OU=7 - Controlled Users,DC=ravenet,DC=rcs"
foreach ($i in $mailbox)
[string]$forwardingaddress = ($i.primarysmtpaddress); $forwardingaddress= $forwardingaddress.Replace("ravenna.portage.k12.oh.us", "ravennaschools.us")
Get-Mailbox $i.primarysmtpaddress |Set-Mailbox -ForwardingAddress $forwardingaddress -DeliverToMailboxAndForward $true

I get this for error results:
Cannot process argument transformation on parameter 'ForwardingAddress'. Cannot convert value "" to type "Microsoft.Exc
hange.Configuration.Tasks.RecipientIdParameter". Error: "Parameter values of type Microsoft.Exchange.Configuration.Task
s.RecipientIdParameter can't be empty. Specify a value, and try again.
Parameter name: identity"
    + CategoryInfo          : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox

Invoke-Command : Cannot write input as there are no more running pipelines
At C:\Users\administrator.RAVENET\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\rsd-e2k10.ravenet.rcs\rsd-e2k10.r
avenet.rcs.psm1:49196 char:29
+             $scriptCmd = { & <<<<  $script:InvokeCommand `
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], PSInvalidOperationException
    + FullyQualifiedErrorId : NoMoreInputWrite,Microsoft.PowerShell.Commands.InvokeCommandCommand

Running the get-mailbox gives:
Name                          PrimarySmtpAddress            ForwardingAddress                DeliverToMailboxAndForward
----                          ------------------            -----------------                --------------------------
tester, etest1                etest1@ravenna.portage.k12...                                                       False
tester, etest2                etest2@ravenna.portage.k12...                                                       False

What did I do wrong? (and thank you for helping me)!
Joshua GrantomSenior Systems AdministratorCommented:
before your switch to google your email looked like this?

Stephen YorkAuthor Commented:
yes!  And now it looks like bob.smith@ravennaschools.us...
Stephen YorkAuthor Commented:
I found this script on the web and see that it was modified.  The mod was not correct, but you pointed me in the right direction and I solved the issue -

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.