The Where-Object
cmdlet is used to filter the resulting data through a pipeline.
We'll call Where-Object
after the |
pipe operator. We can access the current object in the script block using $_
:
Where-Object { condition }
This command does not take any parameters. It only accepts conditions to filter the output data from the pipeline.
This method returns the filtered data.
In the following example, we'll filter the students who have more than five character lengths in their names.
#!/usr/bin/pwsh -Command#given array of students$students = @('Daniel', 'Westin', 'Tadeo', 'Hezekiah')#filter students using Where-Object$students | Where-Object { $_.Length -gt 5}
In the above code snippet:
students
.Where-Object
. We access the current object using $_
and get the length using the in-built Length
property, and we compare it using the comparison operator called -gt
.