Bulk update ProxyAddresses in AD using UserPrincipalName | Set-Aduser

ad+1

A colleague asked for help in updating proxy addresses for multiple users in the local AD. Easy to guess, PowerShell was the answer for bulk changes. Challenge was they only had UPNs or UserPrincipalName and ProxyAddresses available with them which made it slightly non-standard. 

Typically, the scripts you run in the AD use 'sAMAccountName' to identify the users uniquely. They didn't have it. And we did not want to run multiple other processes to get sAMAccountName for all the UPNs. 

On a side note, it is quite a straight-forward task to update attributes for multiple users in the Active Directory using PowerShell. Most attributes can be manipulated easily, however updating ProxyAddresses can be a challenge. This is because it is a multi-valued attribute capable of holding different values.

So, is it possible to update ProxyAddresses for users in bulk using PowerShell with starting with just the UPNs? Yes - just follow the below mentioned steps. If you already have a list of users and the proxy addresses you want to set for them, go straight to step 4. :)

# Step 1: Get a list of users to change. Save the file as C:\Temp\UserList.csv



# Checking Preview of the CSV File in PowerShell

Set-Location C:\temp

Import-Csv UserList.csv 



# Step 2: Check current ProxyAddresses - GET ONLY (Optional step) | Save CSV

Import-Csv UserList.csv | ForEach {

  Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'" -Properties SamAccountNAme, ProxyAddresses, UserPrincipalName `

  | Select-Object UserPrincipalName, SamAccountNAme, @{L = "ProxyAddresses"; E = { $_.ProxyAddresses -join ";"}} `

  | Export-Csv -Path C:\Temp\CurrentProxyAddresses.csv -NoTypeInformation -Append

  } 





# Step 3: Edit the exported file with desired changes to the CSV file. Save it as "BulkUpdateProxyAddresses.csv" Use Notepad or Excel to edit it.



# Step 4: See preview of the new file 

Import-Csv BulkUpdateProxyAddresses.csv 



# Step 5: Set ProxyAddresses according to file - REPLACE Mode

Import-Csv BulkUpdateProxyAddresses.csv | ForEach {

Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'" -Properties SamAccountNAme, ProxyAddresses, UserPrincipalName `

  | Set-ADUser -Replace @{ProxyAddresses= $($_.ProxyAddresses -split ";") } `

  }



# Step 6: Finally, see the change in local AD and or cmdlet used in step 2 (Remember to use a different filename this time e.g. CurrentProxyAddresses-UPDATED.csv  )

Import-Csv UserList.csv | ForEach {

  Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'" -Properties SamAccountNAme, ProxyAddresses, UserPrincipalName `

  | Select-Object UserPrincipalName, SamAccountNAme, @{L = "ProxyAddresses"; E = { $_.ProxyAddresses -join ";"}} `

  | Export-Csv -Path C:\Temp\CurrentProxyAddresses-UPDATED.csv -NoTypeInformation -Append

  } 




Great job! Now verify the changes are visible in the local AD.



0 comments:

I welcome you to write your comments here..