Solved

Powershell--Change Primary or External smtp email Domain

Posted on 2014-04-03
15
3,359 Views
Last Modified: 2014-04-08
Please help with scripting changing the contact's primary or external smtp email domain of all in an OU.

Thank you!
0
Comment
Question by:K Anthony O365
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 15

Expert Comment

by:jrhelgeson
ID: 39975273
What you do is create a new email address policy and apply it to an OU.  That is what EAP is for.
0
 
LVL 16

Assisted Solution

by:Enphyniti
Enphyniti earned 500 total points
ID: 39975277
Should it retain any existing email addresses or aliases?  Or just reset the email?  

Is the address generated off of a standard, or do you want to recycle the characters to the left of the @ symbol in their existing email address?
0
 

Author Comment

by:K Anthony O365
ID: 39975322
Sorry for not being more specific:

The addresses were not created via Address Policy. They were created manually. Therefore, no standard.

I simply want to keep the characters on the left side of @, but change domain name;  i.e @contoso.com to @comp.com

I've already move the users to a specific OU
0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 16

Expert Comment

by:Enphyniti
ID: 39975332
Do you want to keep existing addresses, or replace them?
0
 

Author Comment

by:K Anthony O365
ID: 39975352
Replace them.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39975384
Import-Module -Name ActiveDirectory
$OU = "OU=Name,DC=xxx,DC=XXX"

$usernames = Get-Aduser -Filter * -SearchBase $OU -Properties GivenName,SN

foreach ($username in $usernames)
{

# New email address will be "firstname.lastname@yourdomain.com"
$NewEmailaddress = $username.givenname + "." + $username.sn + "@Yourdomain.com"

#if you want to add as an extra email address, please remove the # on next line
#Set-Mailbox -identity $username.displayname -EmailAddresses @{add=$NewEmailaddress}

#if you want to set it as primary address, please remove the # on next line
#Set-Mailbox -identity $username.displayname -EmailAddresses $NewEmailAddress -EmailAddressPolicyEnabled $False

}
0
 
LVL 16

Expert Comment

by:Enphyniti
ID: 39975416
HIGHLY recommend you test this on a single user in a sub OU since I'm writing it off the top of my head.  let me know if you get any syntax errors or whatnot and I'll do my best to address them.

make sure you change the OU path as well.

$newdomain = "newDomain.com"
$oldDomain = "oldDomain.com"

foreach ($mb in (Get-Mailbox -OrganizationalUnit domain.com/path/to/OU -resultsize unlimited | ?{$_.primarysmtpaddress -ilike $oldDomain) {
     $userID = $mb.primarySmtpAddress.split("@")[0]
     set-mailbox $mb.alias -EmailAddresses "SMTP:$($userId)@$($newdomain)"
}
0
 

Author Comment

by:K Anthony O365
ID: 39975435
Correction: I like to keep the current smtp address and just add a new one: For example

current: Joe_smith@contoso.com
new: joe_smith@comp.com

Since there is no standard, firstname and lastname attributes cannot be used.
0
 
LVL 16

Accepted Solution

by:
Enphyniti earned 500 total points
ID: 39975459
To confirm, at the end of the script should Joe Smith have only joe_smith@comp.com, or should he have a Primary address of Joe_Smith@comp.com and an alias address of joe_smith@contoso.com

If you want to keep both addresses, DON'T run my example above.

If you only want the single address with the new domain, then this will work.  (above has typo)

$newdomain = "newDomain.com"
$oldDomain = "oldDomain.com"

foreach ($mb in (Get-Mailbox -OrganizationalUnit domain.com/path/to/OU -resultsize unlimited | ?{$_.primarysmtpaddress -ilike $oldDomain)) {
     $userID = $mb.primarySmtpAddress.split("@")[0]
     set-mailbox $mb.alias -EmailAddresses "SMTP:$($userId)@$($newdomain)"
} 

Open in new window

0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39975464
This will work for what you needed.

Import-Module -Name ActiveDirectory
$OU = "OU=Name,DC=xxx,DC=XXX"

$usernames = Get-Aduser -Filter * -SearchBase $OU -Properties GivenName,SN

foreach ($username in $usernames)
{

# New email address will be "existingemail@yournewdomain.com"
$NewEmailaddress = (Get-CasMailbox -Identity $username.displayname).PrimarySmtpAddress.Local + "@yourdomain.com"

Set-Mailbox -identity $username.displayname -EmailAddresses $NewEmailAddress -EmailAddressPolicyEnabled $False

}

Open in new window

0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39975479
placed a # sign in front of set-mailbox and $Newemailiaddress will display the email address that will assign, so you can test it without changing anything at this point.

Import-Module -Name ActiveDirectory
$OU = "OU=Name,DC=xxx,DC=XXX"

$usernames = Get-Aduser -Filter * -SearchBase $OU -Properties GivenName,SN

foreach ($username in $usernames)
{

# New email address will be "existingemail@yournewdomain.com"
$NewEmailaddress = (Get-CasMailbox -Identity $username.displayname).PrimarySmtpAddress.Local + "@yourdomain.com"

$NewEmailaddress
#Set-Mailbox -identity $username.displayname -EmailAddresses $NewEmailAddress -EmailAddressPolicyEnabled $False

} 

Open in new window

0
 

Author Comment

by:K Anthony O365
ID: 39975610
Enphyniti,

Here is the error I get:


You must provide a value expression on the right-hand side of the '-' operator.
At C:\Users\modContacts.ps1:4 char:115
+ foreach ($mb in (Get-Mailbox -OrganizationalUnit '***********/Contacts/********** | ?{$_.primarysmtpaddress - <<
<< islike '$oldDomain') {
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39975664
Review the original code again: You made several non-operational changes.
0
 

Assisted Solution

by:K Anthony O365
K Anthony O365 earned 0 total points
ID: 39976013
Yes, I do see the operation changes. I also see how Enphyniti's script would have worked, but I decided to use the Set-mailcontact command as the following:

$Users = Import-CSV 'C:\contacts.csv'
ForEach($User in $Users)
{
write-host "Processing $($User.alias)"
set-mailcontact -identity $user.alias -ExternalEmailAddress $user.email
}

This took care of my needs. Thanks!
0
 

Author Closing Comment

by:K Anthony O365
ID: 39985546
Thanks for the help!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question