Where
- Filter with custom scriptblock by
-FilterScript
- Filter by property by
-Property
- Tons of overloads for binary operators for mimicking direct expression.
NOTE
Use ?
or where
alias for Where-Object
Filter By
- Property
ps1
# -Property is positional
gci -file | where -Property Exists
1
2
2
- Script block
ps1
# -FilterScript is positional
gci -file | where -FilterScript { $_.CreationTime -gt [DateTime]::Now.AddDays(-5) }
1
2
2
Expression-Like
Where-Object
contains a lot of parameters to emulate expression-like syntax with -Property
.
ps1
gci -file | where Extension -eq '.ps1'
# equivalent to
gci -file | where { $_.Extension -eq '.ps1' }
1
2
3
2
3
Intrinsic Where
Intrinsic Where
can be useful when performance matters, it provides a way to return early base on certain condition without consuming the whole iteration.
- First or Last items satisfy certain condition
- Skip until one satisfies the condition and return all remaining items(including the one satisfies)
- Return items until one not satisfies the condition(excluding the one satisfies)
- Split items into two collections, one contains items satisfied the condition, the another are items remained.
cs
Where(scriptblock condition, WhereOperatorSelectionMode mode = 0, int? count)
1