用于过滤具有多个值的记录的 PowerShell 命令

标签 powershell

我正在使用 PowerShell 命令。我想过滤具有一个字段名称描述的记录。我成功过滤了一个名为“学校”的描述。 我的命令是:

Get-ADuser -filter {(Description -eq "school")} -Properties * | select *

但我想过滤具有多个描述值的记录,如“学校”、“学院”等。这怎么可能?

最佳答案

您可以使用 -or 语句:

Get-ADuser -filter {(Description -eq "school") -or (Description -eq "college")} -Properties * | select *

或者您可以创建一个数组并过滤结果,尽管这是在查询执行后进行过滤,因此可能需要更长的时间。在通过 where-object 传递之前,尝试将过滤器应用于 Get-AdUser 是有意义的:

@filter = @("school", "college")
Get-ADuser -Properties * | where-object{@filter -contains $_.Description} | select *

关于用于过滤具有多个值的记录的 PowerShell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24504601/

相关文章:

windows - 同步windows文件夹

powershell - 在此处字符串中使用变量时如何保留换行符

powershell - 我可以防止 PowerShell 脚本删除移动和剪切的文件吗?

powershell - 无法使用 SetEnvironmentVariable 设置 PATH

java - 如何从 Java 程序内部创建 powershell 远程 session ?

powershell - 在字符串列表上使用Where-Object的最简单的PowerShell语法

function - 如何在 Powershell 脚本中创建函数?

windows - Powershell 更新 IIS 绑定(bind)

visual-studio-2010 - 如何安装 powershell 作为先决条件?

powershell - 在ForEach-Object中使用PowerShell -is运算符时始终匹配PSCustomObject