简体   繁体   English

PowerShell如何使用-和Where-Object

[英]PowerShell How to use -and in Where-Object

I want to filter a command result using two conditions. 我想使用两个条件来过滤命令结果。 Here are my commands 这是我的命令

 $list=Get-PnpDevice | Sort-Object -Property Name | Where-Object -Property ConfigurationFlags -NotLike '*DISABLED*' | ft Name, InstanceId -AutoSize

and the next filter is 下一个过滤器是

 $list=Get-PnpDevice | Sort-Object -Property Name | Where-Object -Property FriendlyName -like '*touch screen*' | ft Name, InstanceId -AutoSize

both of them works separately but I want to join them using and command. 他们两个都是分开工作的,但是我想使用and命令加入他们。 I tried to use -AND as following command but it keeps raising errors 我试图使用-AND作为以下命令,但它不断引发错误


 Get-PnpDevice | Sort-Object -Property Name | Where-Object{
      ( ConfigurationFlags -NotLike '*DISABLED*') -and 
      ( FriendlyName -like '*touch screen*' ) }| ft Name, InstanceId -AutoSize

Simply use the The $_ automatic variable in your Where-Object to reference the property names: 只需在Where-Object使用$_ 自动变量来引用属性名称:

Get-PnpDevice | Sort-Object -Property Name | Where-Object{
      ( $_.ConfigurationFlags -NotLike '*DISABLED*') -and 
      ( $_.FriendlyName -like '*touch screen*' ) }| ft Name, InstanceId -AutoSize

您可以将“ Where”子句通过管道传递在一起……语法更简单,更易于阅读

Get-PnpDevice | Sort-Object -Property Name | Where ConfigurationFlags -NotLike '*DISABLED*' | Where FriendlyName -like '*touch screen*' | ft Name, InstanceId -AutoSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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