Solved

Powershell--Change Primary or External smtp email Domain

Posted on 2014-04-03
15
3,312 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
This video discusses moving either the default database or any database to a new volume.

831 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