简体   繁体   中英

Remove full email with specific domain in PowerShell using Get-AzureADUser

I am currently using the below script.

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

Most of my results return an email address in the array similar to email@domain.onmicrosoft.com . I need these emails to be removed from the array in the Aliases (ProxyAdresses) field. Essentially, any email containing onmicrosoft.com needs to be removed.

Current result: email1@domain.com;email2@domain.com;email3@domain.onmicrosoft.com

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

You can chain another operator on there, to work as a filter:

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

but it's getting increasingly difficult to read now. Note that the expression is a full scriptblock, so you can break it out over multiple lines and write things more clearly, eg:

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

            $filteredMails = $_.ProxyAddresses | Where-Object { 
                $_ -notlike '*onmicrosoft*'
            }

            $filteredMails = $filteredMails -replace '^smtp:'

            $filteredMails -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