Home > Blockchain >  Getting specific smtp address from proxy address attribute via powershell
Getting specific smtp address from proxy address attribute via powershell

Time:10-20

I want to get only addresses starting with nby.

I have proxyAddresses for users like below.

sAMAccountName,ProxyAddresses
user01,SMTP:[email protected];smtp:[email protected];smtp:[email protected]
user02,SMTP:[email protected];smtp:[email protected];smtp:[email protected]
....
so on

script :

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,[email protected]
user02,[email protected]
....
so on

CodePudding user response:

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.

  • Related