. Advertisement .
..3..
. Advertisement .
..4..
Sometimes you’ll need a means to filter out everything you don’t need when working with property values of Windows Powershell in a set of objects. This is when the Powershell Where-Object comes in handy. Don’t know how to use this command? This guide will teach you how.
What Is Powershell Where-Object?
The cmdlet Where-Object in Windows PowerShell is designed solely to filter the command’s output and give back particular data that users wish to print. In short, this cmdlet is a type of filter, allowing you to create a condition that only produces either False or True. It either gives back the result or not, depending on the situation.
You have two options for creating this condition: parameter and script blocks.
Use Powershell Script Blocks To Create The Filter Condition
In PowerShell, script blocks are one crucial element. These are employed in countless places in the script language. The script block is a type of anonymous function which organizes code and runs it in several locations.
Below is an example of a script block to help you better understand.
{$_.StartType -EQ 'Automatic'}
Utilizing the Powershell pipeline, users may pipe the items to the cmdlet Where-Object
and utilize the parameter FilterScript. Given that FilterScript takes script blocks, you can establish a condition to determine if each object’s property equals a particular value.
Here is a sample.
Get-Service | Where-Object -FilterScript {$_.StartType -eq 'Automatic'}
Notice that you may utilize the cmdlet Where-Object
with positional parameters and not include the name of the parameter FilterScript. For a quicker and clearer approach of scripting, you instead offer a script block by itself, such as Where-Object {$_.StartType -eq ‘Automatic’}
.
Although this particular syntax is effective in this situation, the idea of script blocks with curly braces {} will make the code harder to read and understand for Windows PowerShell beginners. This problem of readability made the Powershell team introduce the parameters.
Use Powershell Parameter To Create The Filter Condition
Since PowerShell 3.0, the parameters now have a more natural writing style. Let’s utilize parameters to filter certain outputs in the same example as before.
Here is the sample code.
Get-Service | Where-Object -Property StartType -eq 'Automatic'
As you can see above, the property of the object is specified by the command as one parameter value to the Property parameter rather than utilizing a script block. Additionally, the operator -eq
is a parameter, enabling you to send the value Automatic to it.
A script block is no longer required when utilizing parameters. Still, even if Microsoft offered parameters as a simpler form of scripting, there are many benefits to using script blocks.
Utilize Multiple Conditional Statements
With script blocks, you may combine and evaluate more than two conditions using an operator (such as –or or –and). Still, in order to effectively assess each condition, you must employ a specific PowerShell feature called subexpressions.
Subexpressions may be used to execute an expression inside of a filter syntax such as Where-Object. To accomplish this, enclose conditional statements you are running in parenthesis ().
Below is a sample code.
Get-Process | Where-Object {($_.CPU -gt 2.0) -and ($_.CPU -lt 10)}
Here is an example of the script block.
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
334 21 18972 26384 5.23 3808 0 AnyDesk
635 34 13304 51264 9.56 4140 5 notepad
726 36 12820 51196 4.69 12088 5 notepad
802 46 18356 65088 7.98 10784 5 OneDrive
340 18 6472 26436 3.44 1252 5 RuntimeBroker
698 34 14672 44484 3.63 3284 5 RuntimeBroker
323 19 5732 23432 4.00 11200 5 RuntimeBroker
560 18 6440 27752 4.63 8644 5 sihost
632 31 21524 69972 2.81 6392 5 StartMenuExperienceHost
390 18 9756 30832 3.94 3084 5 svchost
523 24 8768 36312 2.17 6300 5 svchost
524 17 6416 21036 4.42 10932 5 SynTPEnh
525 22 15336 38904 3.41 2192 5 TextInputHost
The Bottom Line
Now you know how to utilize Powershell Where-Object. Hopefully, you will find this guide easy to understand and follow.In case you aren’t familiar with the commands in Powershell, maybe you should start with this guide on the cmdlet Start Process. This command is quite useful when working with this language scripting.
Leave a comment