Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting specific smtp address from proxy address attribute via powershell

I want to get only addresses starting with nby.

I have proxyAddresses for users like below.

sAMAccountName,ProxyAddresses
user01,SMTP:user01@domainA.com;smtp:user01@domainA.com;smtp:nby24048@domainA.com
user02,SMTP:user02@domainA.com;smtp:user02@domainA.com;smtp:nby44048@domainA.com
....
so on

script :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Get-Content "C:\Scripts\employeelist.txt" |
  ForEach-Object { Get-ADUser -LDAPFilter "(mail=$_)" -Properties * } |
  Select-Object sAMAccountName,mail,@{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -match '^smtp:') -join ";"}} |
  Export-Csv "C:\Scripts\users-output.csv" -NoTypeInformation

My desired output:

sAMAccountName,ProxyAddresses
user01,nby24048@domainA.com
user02,nby44048@domainA.com
....
so on

>Solution :

Assuming your input text file contains email adresses each on a separate line, you can do this:

# the Where-Object is just to make sure you don't read in blank lines
Get-Content -Path "C:\Scripts\employeelist.txt" | Where-Object { $_ -match '\S' } | ForEach-Object { 
    Get-ADUser -LDAPFilter "(mail=$_)" -Properties ProxyAddresses |
    Select-Object SamAccountName,
                  @{Name = "ProxyAddresses"; Expression = { ($_.ProxyAddresses -match '^smtp:nby.+') -join ";"}}
} | Export-Csv "C:\Scripts\users-output.csv" -NoTypeInformation

P.S. Don’t use -Properties * if you only want a few extra properties Get-ADUser does not already provide.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading