Solved

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

Posted on 2015-02-04
4
1,929 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…

830 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