Home > Blockchain >  Powershell; Filter all ProxyAddresses with a number; Remove SMTP from output file
Powershell; Filter all ProxyAddresses with a number; Remove SMTP from output file

Time:10-25

So I have this mostly right, but I'm not sure how to string other filters into this oneliner.

How would I filter any email address that has a number in it?

How would I add -replace to remove SMTP on the output file?

Thanks all, appreciate it :)

Get-ADUser -Filter * -Properties proxyaddresses,Department,LastLogonDate  | Select-Object  @{Name="LastLogonDate";Expression={$_.LastLogonDate.ToShortDateString()}},Name,Department, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -cmatch '^SMTP:') -join ";"}}|Export-Csv -Path C:\Temp\AdUsersProxyAddresses.csv -NoTypeInformation

CodePudding user response:

If I'm understanding correctly, you have for example:

$proxyAddresses = @(
    'SMTP:[email protected]'
    'smtp:[email protected]'
    'smtp:[email protected]'
)

And you want to list only addresses that do not contain any number and also remove the starting smtp:, in which case, using the example above:

PS ..\> ($proxyAddresses -cmatch '^SMTP:|^\D $') -replace '^smtp:' -join ';'

[email protected];[email protected]

If that's correct, then the expression would be:

@{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -cmatch '^SMTP:|^\D $') -replace '^smtp:' -join ';' }

CodePudding user response:

You can add one more SELECT-OBJECT with the expression for replace

Get-ADUser -Filter * -Properties proxyaddresses,Department,LastLogonDate  | 
Select-Object  @{Name="LastLogonDate";Expression={$_.LastLogonDate.ToShortDateString()}},Name,Department, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -cmatch '^SMTP:') -join ";"}} |
Select-object LastLogOnDate, Name,Department, @{N="ProxyAddresses";E={$_.ProxyAddresses.replace("SMTP:","")}}
Export-Csv -Path C:\Temp\AdUsersProxyAddresses.csv -NoTypeInformation
  • Related