PowerShell Script to Forward Email for a Bulk Set of Users

I wrote a PowerShell script for an Exchange 2007/2010 migration that will forward email for a group of users to an external SMTP recipient, while also delivering mail to the user’s inbox.  The script uses an input csv file which specifies the following: the users that need their email forwarded, the address to forward the mail to, and the name of the contact that will be created in the source domain.

The script creates the contacts, forwards the email to the contacts, enables delivery to both the source user and the target contact, and hides the contact from the GAL.
Enjoy!
#####################################################
# Bulk Contact Creation and Mail Forward Script #
# for Exchange 2007/2010 #
# Ron Williams #
# ron dot williams at mail dot com #
# 3/20/13 #
#####################################################

#INSTRUCTIONS:
# 1. Update csv file with appropriate values, make sure to the keep header row. 
# SamAccountName is the SAMaccountName from the source domain of the user 
# whose mail should forward to ext recipient.
# ForwardingAddress is the SMTP address to forward the mail to. 
# Will existing mailbox with this value as -ForwardingAddress
# FirstName is the First Name value that will be created on the contact
# LastName is the Last Name value that will be created on the contact
# 2. Run from EMS in format "C:\pathname\BulkContactForward.ps1 C:\pathname\input.csv"
# 3. Update $OU in variables section below to the OU where you want to creat the contacts.

#CSV FILE FORMAT:
# SamAccountName,ForwardingAddress,FirstName,Lastname
# joeb,joe.blow@contoso.com,Joe,Blow
# mikes,michael.smith@contoso.com,Mike,Smith

#VARIABLES
$OU="contoso.com/TestOU"

# Loop through the object returned by Import-Csv with each element represented by $person
foreach ($person in (Import-Csv .\input.csv))
 {
 # Check the Mailbox for the person exists
 If ((Get-Mailbox $person.SamAccountName -ErrorAction SilentlyContinue)) 
 { 
 # Check the mail contact doesn't exist and if not add it, hide it from GAL
 If (!(Get-MailContact $person.SamAccountName -ErrorAction SilentlyContinue)) 
 {
 New-MailContact -name $person.SamAccountName -Firstname `
 $person.FirstName -Lastname $person.lastname `
 -ExternalEmailAddress $person.ForwardingAddress -OrganizationalUnit $OU;
 Set-MailContact -identity $person.SamAccountName `
 -HiddenFromAddressListsEnabled $true
 Set-Contact $person.SamAccountName -Notes `
 "Created for Email Migration Coexistence"
 # Set the Forwarding Address on the Mailbox 
 Set-Mailbox $person.SamAccountName -ForwardingAddress `
 $person.ForwardingAddress -DeliverToMailboxAndForward $true 
 }
 else {write-host "The contact for", $person.SamAccountName, "already `
 exists. No furthur action on this contact will be performed."}
 }
 else {write-host "The mailbox for", $person.SamAccountName, "did not exist. `
 No furthur action on this mailbox will be performed."}
}

Error c1034a7f When Attempting To Remove Exchange Installation

from http://support.microsoft.com/kb/555147

SUMMARY

You receive an error message informing you that one or more users still have a mailbox on an Exchange server that you are trying to uninstall.

 

SYMPTOMS

When attempting to decommission an Exchange server by running the setup program to remove the installation, you receive the following error:

 

One or more users currently use this mailbox store. These users must be moved to a different mailbox store or be mail disabled before deleting this store.

ID no: c1034a7f

Exchange System Manager

 

This is despite the fact that you think you have already removed all the mailboxes off of this server.  You follow the instructions in Microsoft Knowledgebase article 279202 but this makes no difference.

 

 

CAUSE

It is likely that there are still some objects with Active Directory that have Exchange attributes configured.  These need to be removed before the Exchange server can be uninstalled.

 

 

RESOLUTION

To find the objects with Exchange attributes, use the Active Directory Users & Computers snap-in.

 

  1. Run Active Directory Users & Computers.
  2. Right-click your domain at the top of the tree and click Find.
  3. Click the Advanced tab and then choose User from the Field button.
  4. From the list of attributes displayed, choose Exchange Home Server.
  5. Set the Condition field to Ends With and then type the name of your Exchange server into the Value field.  Click Add to add this value.
  6. Click the Find Now button which should now display the problematic objects.

 

The Exchange attributes can now be removed from these objects and the uninstall process attempted again.

Duplicate Email Address Exists in Exchange

Sometimes when you try to add an smtp address to an object in AD, you get the following error due to the fact that the address already exists:

This email address already exists in the organization.  ID no: c10312e7

 

Active Directory Users and Computers snap-in can be used to find the object that owns the address.

 

To do this, follow these steps:

 

1.    Run Active Directory Users and Computers.

2.    Right-click the domain object and choose Find from the context menu. This will bring up the Find Users, Contacts, and Groups window.

3.    In the Find drop-down list, choose Custom Search.

4.    Change the scope to Entire Directory

5.    Click the Advanced tab and in the Enter LDAP query: field, enter the following text: proxyaddresses=smtp:rwilliams@zixcorp.com  (use the email address that you are trying to add).  Notice that the attribute proxyaddresses is plural, i always forget that…

6.    Click the Find Now button and it will show you the object that owns the SMTP address.  It’s usually a public folder in my experiences.