Schmit
Schmit

Reputation: 51

Pulling User Object's Primary SMTP from ProxyAddresses

I have a PS script that is pulling Employee IDs from a CSV file that I update every morning that I get from HR just to make sure the automation is running correctly by adding/changing their email, extensionAttribute 1, and the ProxyAddresses. I would like it to only check for the Primary SMTP instead of all ProxyAddresses but am having trouble.

Import-Csv "C:\temp\HRfeed101519.csv" | foreach {Get-ADUser $_.EmpID -Properties * | fL mail, extensionattribute1, Proxyaddresses}

Upvotes: 0

Views: 4990

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

The ProxyAddresses field identifies the PrimarySMTPAddress with the SMTP: tag. Therefore, you can query for that specifically and output it as a calculated property.

Get-ADUser $_.EmpID -prop ProxyAddresses,Mail,ExtensionAttribute1 |
    Select-Object Mail,ExtensionAttribute1,ProxyAddresses,
    @{Name='PrimarySMTPAddress';Expression={$_.ProxyAddresses -cmatch '^SMTP:' -creplace 'SMTP:'}}

-cmatch and -creplace perform case-sensitive regex matching.


Note: The default table display of the output may not show all of the properties and values due to collection size stored in ProxyAddresses. You can pipe your output to Format-List to see all properties, but do not store the Format-List output in a variable.

Upvotes: 3

Related Questions