?
Solved

Powershell--Change Primary or External smtp email Domain

Posted on 2014-04-03
15
Medium Priority
?
3,458 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:Enphyniti
Enphyniti 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 16

Expert Comment

by:Enphyniti
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: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: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:
Enphyniti 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 70

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

770 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