Solved

How to bulk replace all attributes for proxyAddresses from Active Directory?

Posted on 2015-02-04
4
2,169 Views
1 Endorsement
Last Modified: 2015-02-04
I'm trying to replace all the proxyAddresses entries from a list in Active Directory.

I have a CSV file with;
SamAccountName, Primary Email Address

but i need to remove all previous entries too

Many thanks
1
Comment
Question by:mitsas
[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
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Waddah Dahah
ID: 40588454
Hi Mitsas,

the below PowerShell script switches the old primary email domain with the new one, and also updates the Mail attribute (What you see next to E-mail Address in the General tab of the user).  It could use some refining, and the logic could be improved for detecting/setting proxyaddresses, but should give you the start you need.

also please ready the following article;
http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/21/weekend-scripter-create-proxyaddresses-in-active-directory-with-powershell.aspx

$olddomain = "@olddomain.com"
$newdomain = "@newdomain.com"
$makealias = $true
$userou = 'OU=Test,DC=domain,DC=local'
$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName, EmailAddress, ProxyAddresses
Foreach ($user in $users) {
    $oldemail = "$($user.samaccountname)$($olddomain)"
    $newemail = "$($user.samaccountname)$($newdomain)"
    Write-Host "User: $($user.samaccountname)`n------------------------"d

    #Update Mail Attribute    
    If ($user.EmailAddress -ieq $oldemail) {
        Write-Host "Mail Attribute: Old Value Detected Updating..."
        Write-Host "Old Value: $($user.EmailAddress)"
        $user.EmailAddress = $newemail
        Write-Host "New Value: $($newemail)"
    }
    Elseif ($user.EmailAddress -ieq "$newemail") {
        Write-Host "Mail Attribute: New Value Detected Skipping..."
        Write-Host "Value: $($user.EmailAddress)"
    }
    Else {
        Write-Host "Mail Attribute: Unknown Value Detected NOT Updating..."
        Write-Host "Value: $($user.EmailAddress)"
    }

    #Update ProxyAddresses Attribute
    $blnPrimaryOld = $false
    $blnPrimaryNew = $false
    $blnPrimaryOther = $false
    $blnAliasOld = $false
    $blnAliasNew = $false
    ForEach ($proxy in $user.ProxyAddresses) {
        If ($proxy.StartsWith("SMTP:")) {
            If ($proxy -eq "SMTP:$($oldemail)") {
                $blnPrimaryOld = $true
            }
            Elseif ($proxy -eq "SMTP:$($newemail)") {
                $blnPrimaryNew = $true
            }
            Else {
                $blnPrimaryOther = $true
            }
        }
        ElseIf ($proxy.StartsWith("smtp:")) {
            If ($proxy -eq "smtp:$($oldemail)") {                
                $blnAliasOld = $true
            }
            Elseif ($proxy -eq "smtp:$($newemail)") { 
                $blnAliasNew = $true
            }
        }
    }
    If (($blnPrimaryOld -eq $true) -AND ($blnPrimaryNew -eq $false) -AND ($blnPrimaryOther -eq $false)) {
        Write-Host "Primary Email: Old Value Detected Updating..."
        Write-Host "Removing SMTP:$($oldemail)"
        $user.ProxyAddresses.remove("SMTP:$($oldemail)")
        Write-Host "Adding SMTP:$($newemail)"
        $user.ProxyAddresses.add("SMTP:$($newemail)")
        Write-Host "Make Old Email Alias: $($makealias)"                
        If ($makealias -eq $true) {
            Write-Host "smtp:$($oldemail)"  
            $user.ProxyAddresses.add("smtp:$($oldemail)")
        }
    }
    Elseif (($blnPrimaryNew -eq $true) -AND ($blnPrimaryOld -eq $false) -AND ($blnPrimaryOther -eq $false)) {
        Write-Host "Primary Email: New Value Detected Skipping..."
    }
    Else {
        Write-Host "Primary Email: Unknown Value Detected NOT Updating..."
    }


    #Write Values to User
    Write-Host "Setting Values..."
    $result = Set-ADUser -Instance $user
    Write-Host "`n"
}

Open in new window

0
 

Author Comment

by:mitsas
ID: 40588461
Thanks, the problem we have is hundreds of bad entries we need to remove for numerous domains.

I've found this command works perfectly
Set-ADUser $_.user -clear proxyAddresses
and it works for a single entry.

How can use this to bulk edit all the users?

Thanks
0
 
LVL 3

Accepted Solution

by:
Waddah Dahah earned 500 total points
ID: 40588511
Try the following please.

$UserList=Get-ADUSER -Filter * -SearchScope Subtree -SearchBase "OU=myou,OU=otherou,DC=mydc,DC=local"
foreach ($User in $UserList) {Set-ADUSER $user -clear proxyAddresses $true}

Open in new window

0
 

Author Comment

by:mitsas
ID: 40588560
A small change:
foreach ($User in $UserList) {Set-ADUSER $user -clear proxyAddresses}

and it works perfectly.

Thank you.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

707 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