How to Dump users “proxyAddresses” attribute with PowerShell

On Active Directory “proxyAddresses” and other multi-valued attributes are not so easy to export to a CSV via the Export-Csv.

This script dump all contact objects inside the specified OU to a CSV File:

ipmo activedirectory

$contactou = "OU=Contacts,OU=Domino Objects,DC=MYAD,DC=LOCAL"
$datesuffix = get-date -Format yyy-MM-dd_HHmmss

$allcontacts = get-adobject -filter {objectclass -eq "contact" } -searchbase $contactou -property DistinguishedName,ObjectGUID,proxyaddresses

$allcontacts | select DistinguishedName,ObjectGUID,@{Name='proxyAddresses';Expression={[string]::join(";", $($_.proxyAddresses))}} | export-csv -delimiter ";" -notype alldominocontacts_$($datesuffix).csv

This script instead read a CSV file and replace the “proxyAddresses” attribute:

Import-Csv "alldominocontacts.csv" -delimiter ";" | ForEach-Object{
    $guid = $_.ObjectGUID
    $proxyAddresses = $_.proxyaddresses -split ';'
    $find = Get-ADObject -filter {(objectGUID -eq $guid)} -searchbase "OU=Contacts,OU=Domino Objects,DC=MYAD,DC=LOCAL" -Properties Name,ProxyAddresses
    Write-Host "Contact:"
    Write-Host $find.Name
    Write-Host "Current ProxyAddresses:" $find.proxyaddresses
    Write-Host "Old ProxyAddresses    :" $proxyAddresses
    Set-ADObject -Identity $guid -Replace @{proxyAddresses=$proxyAddresses}    
    Write-Host "-----------------"

Keep in mind that I’m using “;” as delimiter.

If you want to export all the proxyAddresses values of the contacts objects with an X500 address you can use LDAPFilter option:

Get-ADObject -SearchBase "OU=Contacts,OU=Domino Objects,DC=MYAD,DC=LOCAL" -LDAPFilter "(&(objectClass=contact)(proxyAddresses=X500:*))" -Properties DistinguishedName,ObjectGUID,proxyaddresses | Select DistinguishedName,ObjectGUID,@{Name='proxyAddresses';Expression={[string]::join(";", $($_.proxyAddresses))}} | Export-Csv -Path .\ContactsWithX500Addresses.csv

If you want to remove an explicit entry:

Get-ADObject -SearchBase "OU=Contacts,OU=Domino Objects,DC=MYAD,DC=LOCAL" -LDAPFilter "(&(objectClass=contact)(proxyAddresses=X500:wrong))" -Properties DistinguishedName,ObjectGUID,proxyaddresses | %{Set-ADObject -Identity $_.ObjectGUID  -Remove @{proxyAddresses='X500:wrong'}}

If you want to remove all the X500 entries:

$users = Get-ADObject -SearchBase "OU=Contacts,OU=Domino Objects,DC=MYAD,DC=LOCAL" -LDAPFilter "(&(objectClass=contact)(proxyAddresses=X500:*))" -Properties DistinguishedName,ObjectGUID,proxyaddresses
foreach ($user in $users)
    $addressesToRemove = @($user.proxyAddresses) -like '*500*'
    if ($addressesToRemove.Count -gt 0)
        Set-ADObject -Identity $user.ObjectGUID -Remove @{proxyAddresses = $addressesToRemove}

To import again exported contacts proxyAddresses:

Import-Csv ContactsWithBrokenX500Addresses.csv | ForEach-Object{
    $guid = $_.ObjectGUID
    $proxyAddresses = $_.proxyaddresses -split ';'
    Set-ADObject -Identity $guid -Replace @{proxyAddresses=$proxyAddresses}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s