I'm being tasked with creating a script that polls AD for specific information and then creates an appropriate email address based on that information. Please allow me to give you a bit of a rundown.

My company has 5 email domains that we have. All 5 are allowed through Exchange, but we only use one right now. Currently, everyone is setup with the default company.com email address. We have several different brands that we'd like to segment our users to in an effort to increase email deliverability to help keep our corporate email unaffected by IP reputation when marketing decides to release mass emails.

I manage a 7 server Exchange 2010 DAG (3 production, 3 DR, 1 3-day Lag). I'm fairly decent at PowerShell and pretty good at finding the code pieces I need, so I'm not looking for an entire script, but I'm having troubles with this certain pieces.

What the business wants to do is have a script that reads an attribute in AD, then based on that attribute, assigns it to the correct brand. So, for instance. If AD user has "Marketing" attribute, they would get marketing.com email address as their primary reply-to address while still retaining their company.com email address as a secondary. If an AD user has "building products" as their attribute, building.com would be their reply-to email address, etc.

I can get PowerShell to poll AD for an attribute. I can get it to create an email. I'm having troubles figure out how to get it to select one of the other domain names and not the default company.com email address.

Any insight would be greatly appreciated.

  • The default SMTP address is the one where SMTP is in all capitals. – longneck Apr 7 '15 at 14:34
  • 1
    Under which property are these attributes? Are they under office? Department? Job Title? – pxed Apr 7 '15 at 14:34
  • @pxed They will be defined by Department. – Dave H. Apr 7 '15 at 14:40

This is untested, but should get you in the right direction.

$peeps = get-aduser -filter * -properties department
for-each ($person in $peeps){

##Checks Department and sets new email

if ($person.department -eq "building"){$email = $person.firstname+'.'+$person.surname +'@buildings.com'}
elseif ($person.department -eq "otheroption"){$email = $person.firstname+'.'+$person.surname +'@whatever.com'}

##Changes existing email addresses to aliases and sets new email from above as primary

$person.user = $user
$smtp=get-aduser $user.samaccountname -properties proxyaddresses |select-object proxyaddresses
    $newsmtp = @()
    foreach($addr in $smtp.proxyaddresses){
        if ($addr -notmatch $email -and $addr -cmatch "SMTP"){
            $newsmtp = $newsmtp + $addr.replace("SMTP","smtp") 
        elseif ($addr -notmatch $email -and $addr -cnotmatch "SMTP"){$newsmtp = $newsmtp + $addr}
    $emailproxy = 'SMTP:'+$email
    $newsmtp = $newsmtp + $emailproxy
    set-aduser $user.samaccountname -replace @{proxyaddresses=@($newsmtp)}

  • that looks exactly like something I can work with. Much appreciated! I'll report back when I get it working. – Dave H. Apr 7 '15 at 14:59
  • Great! Anytime! Feel free to comment back if you get stuck! – pxed Apr 7 '15 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.