简体   繁体   中英

Remove 'SMTP' from 'SMTP:email@domain.com' in PowerShell using Get-AzureADUser

I am using the below script to display aliases for users in O365 which I will eventually be exporting.

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { $_.ProxyAddresses -join ";"}}
    )

In the Aliases ( ProxyAddresses ) column, it displays all the aliases separated by a ; as expected but it also includes SMTP: in front of all of them.

Is there a way to remove the SMTP: from these values?

Current result : SMTP:email@domain.com;SMTP:email2@domain.com

Desired result : email@domain.com;email2@domain.com

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { $_.ProxyAddresses -replace '^smtp:' -join ';' }}
    )

This will replace smtp: at the beginning ( ^ ) of each proxyaddress, using PowerShell's ability to make operators automatically work on all members of an array, then join the results.

If you also want to lose the ".onmicrosoft.com" addresses this should work

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { 
            $proxies = $_.ProxyAddresses -replace '^smtp:'
            $proxies = $proxies | Where-Object { $_ -notlike '*.onmicrosoft.com' }
            $proxies -join '; ' }
        }
    )

Mind you, the ProxyAddresses attribute can also hold other entries like SIP:upn@domain.com or X500: /o=company/ou=External (FYDIBOHF25SPDLT)/cn=Recipients/cn=userxxx

If you also would like to exclude those, use

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { 
            $proxies = $_.ProxyAddresses
            $proxies = $proxies | Where-Object { $_ -match '^smtp:' -and $_ -notlike '*.onmicrosoft.com' }
            $proxies -replace '^smtp:' -join '; ' }
        }
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM