?
Solved

Powershell--Change Primary or External smtp email Domain

Posted on 2014-04-03
15
Medium Priority
?
3,515 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:Anthony K O365
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Jon Brelie
Jon Brelie earned 1500 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:Anthony K 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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 
LVL 16

Expert Comment

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

Author Comment

by:Anthony K 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:Jon Brelie
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:Anthony K 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:
Jon Brelie earned 1500 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:Anthony K 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 71

Expert Comment

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

Assisted Solution

by:Anthony K O365
Anthony K 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:Anthony K O365
ID: 39985546
Thanks for the help!
0

Featured Post

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
Suggested Courses

649 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