I have a PowerShell Script that reads our Active Directory and writes the “extensionAttribute2, EmailAddress, sAMAccountName, UserPrincipalName and proxyaddresses” attributes to a CSV file. Here is the primary portion of the code:
Get-ADUser -SearchBase "something here" -Filter * -Properties extensionAttribute2, EmailAddress, sAMAccountName, UserPrincipalName, proxyaddresses |
Select-Object extensionAttribute2, EmailAddress, sAMAccountName, UserPrincipalName, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -match '^SMTP:') -join ";"}}|Sort ProxyAddresses -descending | Export-CSV -Path $Output -NoTypeInformation
Because each user account has two proxy accounts (that’s another issue for a different time), I want only to show users who meet the following criteria:
'SMTP:[email protected]'
I have tried various formats of the code but have been unable to retrieve the code with only the users meeting the SMTP criteria. Could you please tell me how to properly code this?
Thank you.
3
What you’re looking for is -cmatch
instead of -match
, the PowerShell operators starting with the c
prefix are all case-sensitive. As aside, the -join ';'
might not be needed (leave it for you to test) since user’s should have only one primary SMTP address (uppercase SMTP:
), meaning, the result from the expression $_.ProxyAddresses -cmatch '^SMTP:'
should be a single string.
$getADUserSplat = @{
SearchBase = 'something here'
LDAPFilter = '(proxyAddresses=SMTP:*@domain.mail.onmicrosoft.com)'
Properties = 'extensionAttribute2', 'EmailAddress', 'sAMAccountName', 'UserPrincipalName', 'proxyaddresses'
}
Get-ADUser @getADUserSplat |
Select-Object @(
'extensionAttribute2'
'EmailAddress'
'sAMAccountName'
'UserPrincipalName'
@{ L='ProxyAddresses'; E={ ($_.ProxyAddresses -cmatch '^SMTP:') -join ';' }}) |
Sort-Object ProxyAddresses -Descending |
Export-Csv -Path $Output -NoTypeInformation
2