Home > Net >  How to manipulate proxyaddresses attribute with get-adobject
How to manipulate proxyaddresses attribute with get-adobject

Time:10-19

I am trying to export a list of contacts using get-adobject and manipulate the proxyaddresses attribute to only give me SMTP:e-mailaddress (the mail attribute). Right now with my command, I get everything including the x500 stuff which I do not want. Is this possible? Thanks.

Get-ADObject -Filter 'objectClass -eq "contact"' -Properties * -SearchBase 'DC=A' | 
select name, givenName,sn,mail,displayName,cn,co,company,l,mailnickname,telephoneNumber,st,streetAddress,postalcode,physicalDeliveryOfficeName,mobile,department,title,proxyaddresses,targetaddress

Did a little Googling, and this seems to work well. Is there a better way of coding?

Select-Object Name, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -like 'smtp:*') -join ";"}}

CodePudding user response:

I know the syntax displayed below is pretty ugly but I really recommend you to avoid -Properties * when you only need a specific set of attributes to query, by doing so your query will run faster and also it will lessen the computational cost.

As for getting only the Primary SMTP Address in the proxyAddresses attribute, PowerShell comparison operators are by default case-insensitive, however they all have a case-sensitive counter-part. See Common Features for details. In this case you can use -clike to filter the uppercase SMTP address (Primary Address).

$propsOfInterest = @(
    'name'
    'givenName'
    'sn'
    'mail'
    'displayName'
    'cn'
    'co'
    'company'
    'l'
    'mailnickname'
    'telephoneNumber'
    'st'
    'streetAddress'
    'postalcode'
    'physicalDeliveryOfficeName'
    'mobile'
    'department'
    'title'
    'proxyaddresses'
    'targetaddress'
)

Get-ADObject -Filter 'objectClass -eq "contact"' -Properties $propsOfInterest -SearchBase 'OU=SomeOU' |
    Select-Object @(
        $propsOfInterest[0]                                              # this is the first `Name`
        @{N='proxyAddresses'; E={ $_.proxyAddresses -clike 'SMTP:*' }}   # calculated property
        $propsOfInterest[1..$propsOfInterest.Count] -ne 'proxyAddresses' # the rest, excluding `proxyAddresses`
    )
  • Related