You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Get-AzureADUser返回的ProxyAddresses中剔除.onmicrosoft.com地址

方案1:拼接ProxyAddresses前直接过滤目标地址(更推荐)

你不需要用Where-Object过滤整个用户对象,只需要在计算属性的表达式里,先对单个用户的ProxyAddresses数组做条目过滤,再拼接即可,写法如下:

$Users = Get-AzureADUser -All:$true | Select-Object DisplayName, mail, @{
    n="ProxyAddresses";
    e={ ($_.ProxyAddresses | Where-Object { $_ -notmatch '\.onmicrosoft\.com' }) -join "`r`n" }
}

这种写法只会移除单个用户ProxyAddresses里带.onmicrosoft.com后缀的条目,不会过滤掉整个用户对象,就算某个用户的所有别名都是.onmicrosoft.com后缀,也只会把该字段留空,用户条目仍会正常保留。


方案2:使用-replace移除目标地址

如果要使用replace操作实现需求,可以直接在拼接后的字符串上做正则替换,注意匹配规则要连带处理前后可能存在的换行符/逗号,避免残留空行或多余分隔符:

适配换行分隔的写法:

$Users = Get-AzureADUser -All:$true | Select-Object DisplayName, mail, @{
    n="ProxyAddresses";
    e={ ($_.ProxyAddresses -join "`r`n") -replace '(?m)^.*\.onmicrosoft\.com.*\r?\n?', '' }
}

适配逗号分隔的写法:

$Users = Get-AzureADUser -All:$true | Select-Object DisplayName, mail, @{
    n="ProxyAddresses";
    e={ ($_.ProxyAddresses -join ', ') -replace ',?\s*[^,]*\.onmicrosoft\.com[^,]*', '' -replace '^, ', '' }
}

内容的提问来源于stack exchange,提问作者Elly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 22:15:07