Solved

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

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

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

708 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

17 Experts available now in Live!

Get 1:1 Help Now