Solved

Powershell--Change Primary or External smtp email Domain

Posted on 2014-04-03
15
3,275 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:kt KTAdmin
  • 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:kt KTAdmin
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
 
LVL 16

Expert Comment

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

Author Comment

by:kt KTAdmin
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:kt KTAdmin
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:kt KTAdmin
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 68

Expert Comment

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

Assisted Solution

by:kt KTAdmin
kt KTAdmin 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:kt KTAdmin
ID: 39985546
Thanks for the help!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
A procedure for exporting installed hotfix details of remote computers using powershell
In this video we show how to create a Contact 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 Recipients >> Contact ta…
To show how to create a transport rule 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 Mail Flow >> Rules tab.:  To cr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now