Examples of the usage of PowerShell Where-Object
The FilterScript value is a script block that evaluates to True or False, that is, one or more Windows PowerShell commands surrounded by curly braces {}. These script blocks may be very simple, but you need to understand another concept about Windows PowerShell, the comparison operator, to create them. The comparison operator compares the items displayed on each side of it. The comparison operator begins with the "-" character, followed by the name. The basic comparison operator applies to almost any type of object. More advanced comparison operators may apply only to text or arrays.
Here are some sample commands, from simple to complex
Get-WmiObject-Class Win32_SystemDriver | Where-Object-FilterScript {$_ .State-eq 'Running'}
Get-WmiObject-Class Win32_SystemDriver | Where-Object-FilterScript {$_ .State-eq "Running"} | Where-Object-FilterScript {$_ .StartMode-eq "Auto"}
Get-WmiObject-Class Win32_SystemDriver | Where-Object-FilterScript {$_ .State-eq "Running"} | Where-Object-FilterScript {$_ .StartMode-eq "Manual"} | Format-Table-Property Name,DisplayName
Get-WmiObject-Class Win32_SystemDriver | Where-Object-FilterScript {($_ .State-eq 'Running')-and ($_ .StartMode-eq' Manual')} | Format-Table-Property Name,DisplayName
In addition to the comparison operators listed above, there are some logical operators that can also be used with where-object
You can also follow the official Wechat account below for more information.