Amazon Contextual Product Ads

Wednesday, June 8, 2011

Script to Change the Primary SMTP address in Exchange 2007

Throughout the years, I have worked for many companies. One thing that pretty much all of these companies did was to change their Domain Names from time to time. It is a pretty tedious process to change the Primary SMTP address on several users, especially for large companies that decide to rebrand.

If you had to do it manually, it could take several days to even weeks and leaves a lot of room for human error. Luckily, we don't have to do it manually. In Exchange 2007, we can do it with a PowerShell script.

The following script will gather the current Primary SMTP address, strip off the domain, and replace it with the new domain. The old Primary SMTP address will remain attached to the user, but as a secondary address, so email will not be interrupted to that user as senders learn to use the new address.

You will need to run the script in the Exchange Management Shell:

#specify a domain controller to work with
$dc = 'USDC01.domain.com'
#Create an output file to capture the results
$Output = "c:\output.txt"
Out-File $Output -InputObject "OldPrimarySMTPAddress`tNewPrimarySMTPAddress"
#specify the OU you wish to pull the users from
$OU = "domain.com/Users" #replace with the OU you want to work with
#specify the domain of the new SMTP address you want to change it to
$NewDomain = "NDomain.com"
#get a list of all mailboxes in the OU
$list = get-mailbox -OrganizationalUnit $OU -resultsize Unlimited -DomainController $DC
#Iterate through the list
foreach ($user in $list)
{
 $mb = Get-mailbox $user -DomainController $dc
 #capture current primary smtp address
 $SMTP = $mb.PrimarySmtpAddress
 [string]$Local = $SMTP.Local
 [string]$OldDomain = $SMTP.Domain
 [string]$CPSMTP = $Local + "@" + $OldDomain
 #captur new primary smtp address
 [string]$NPSMTP = $Local + "@" + $NewDomain
 #capture the old and the new SMTP addresses to the output file
 [string]$iobject = $CPSMTP + "`t" + $NPSMTP
 Out-File $Output -InputObject $iobject -Append
 #set the new primary smtp address on the mailbox and remove the flag to use the email address policy (if you do not do this, the email address will revert to whatever the policy has set to)
 Set-Mailbox $user -PrimarySmtpAddress $NPSMTP -EmailAddressPolicyEnabled $false -DomainController $DC
}

Hopefully, this script proves as useful to you as it was for me time and again.

6 comments:

  1. Hi Gus

    Extremely useful and works flawlessly.

    I'd like to be able to import the users from a CSV file as we are moving batches of users and a convoluted OU structure and GPO's mean we can't just move the users to a temp OU.

    Any suggestions?

    ReplyDelete
  2. If the CSV file only has the usernames in it (one per line), then the easiest way would be to do the following:

    Comment out:
    $OU = "domain.com/IT/Users"

    and Replace:
    $list = get-mailbox -OrganizationalUnit $OU -resultsize Unlimited -DomainController $DC

    with:
    $list = get-content "c:\userlist.csv"

    This will read in the full list into the $list variable and then the foreach statement will go through each one, find the mailbox associated with it, and then change it.

    ReplyDelete
  3. Thanks Gus, that works too I just need to split the users up as their Aliases are mixed (fname.lname and initiallname)

    ReplyDelete
  4. Hi Gus

    This seems like a very handy script.

    I have a scenario where I would like to change the address of some users that are all part of an AD group. Would it be possible to add another variable that fetches these specific users that both are part of a specified OU and a specified AD security group?

    ReplyDelete
  5. The easiest way would be to import the ActiveDirectory module and then run a check against get-adgroupmember. It would look something like this:

    Import-Module ActiveDirectory
    $gusers = get-adgroupmember "GroupName"
    #replace GroupName with the AD Group Name.
    #specify a domain controller to work with
    $dc = 'USDC01.domain.com'
    #Create an output file to capture the results
    $Output = "c:\output.txt"
    Out-File $Output -InputObject "OldPrimarySMTPAddress`tNewPrimarySMTPAddress"
    #specify the OU you wish to pull the users from
    $OU = "domain.com/Users"
    #replace with the OU you want to work with
    #specify the domain of the new SMTP address you want to change it to
    $NewDomain = "NDomain.com"
    #get a list of all mailboxes in the OU
    $list = get-mailbox -OrganizationalUnit $OU -resultsize Unlimited -DomainController $DC
    #Iterate through the list
    foreach ($user in $list)
    {
    $mb = Get-mailbox $user -DomainController $dc
    $sam = $mb.samaccountname
    foreach ($guser in $gusers)
    {
    $gsam = $guser.samaccountname
    if ($gsam -eq $sam)
    {
    #capture current primary smtp address
    $SMTP = $mb.PrimarySmtpAddress
    [string]$Local = $SMTP.Local
    [string]$OldDomain = $SMTP.Domain
    [string]$CPSMTP = $Local + "@" + $OldDomain
    #captur new primary smtp address
    [string]$NPSMTP = $Local + "@" + $NewDomain
    #capture the old and the new SMTP addresses to the output file
    [string]$iobject = $CPSMTP + "`t" + $NPSMTP
    Out-File $Output -InputObject $iobject -Append
    #set the new primary smtp address on the mailbox and remove the flag to use the email address policy (if you do not do this, the email address will revert to whatever the policy has set to)
    Set-Mailbox $user -PrimarySmtpAddress $NPSMTP -EmailAddressPolicyEnabled $false -DomainController $DC
    }
    }
    }

    ReplyDelete