Friday 30 September 2016

Powershell script - Searching for proxyAddresses attibute in Active Directory

We were working on a email filtering project today and had to search for users who do not have proxyAddresses attribute in their AD accounts.

This script will help you extract all user email addresses(if any) in Active Directory and identify who has the proxyAddresses attribute.

Once you identify who's missing this attribute, you can use the script to populate proxyAddresses.

Step 1: Extract Username, Email Address and their Proxyaddresses Attribute.
Get-ADUser -Filter * -SearchBase "dc=domain,dc=ca" -Properties proxyaddresses,EmailAddress |select samaccountname, EmailAddress, @{L="ProxyAddress_1"; E={$_.proxyaddresses[0]}}, @{L="ProxyAddress_2";E={$_.ProxyAddresses[1]}} | Export-Csv -Path c:\users.csv –NoTypeInformation

Step 2: Open the extracted list. Delete the rows of those users that we don’t need to add the proxy address and those that have it already. Delete the all the columns except for the samaccountname and email address.

Step 3: Get all the users in the list and add the email address to the proxyaddresses.
Import-Csv c:\users.csv | ForEach {Get-ADUser $_.samaccountname | Set-ADUser –Add @{proxyaddresses = 'SMTP:'+($_.EmailAddress)}}


Of course, run at your own risk.
Step 3 can corrupt the AD user if you don’t prep the list from Step 2 properly.

No comments:

Post a Comment