Wherefore Art Thou PowerShell? Multiple Object Pipelines
WhereFor is a small PowerShell module that combines Where-Object and Foreach-Object into a single useful command, Get-WhereFor
.
Using WhereFor, it's simple and straightforward to check for multiple conditions in a single pipeline, thus avoiding repeated passes over the same data.
You can install WhereFor from the PowerShell Gallery with Install-Module:
Install-Module WhereFor -Scope CurrentUser
After installation, you can import the module by name:
Import-Module WhereFor
1..3 | ?% @{
{$_ % 2} = {"$_ is odd"}
{-not ($_ %2)}={"$_ is even"}
}
Get-Process |
WhereFor @{
{ $_.Handles -gt 1kb } = { "$($_.Name) [ $($_.Id) ] has $($_.handles) open handles " }
{ $_.WorkingSet -gt 1gb } = { "$($_.Name) [ $($_.Id) ] is using $($_.WorkingSet) of memory" }
}
"the quick brown fox jumped over the lazy dog" -split '\s' |
Get-WhereFor ([Ordered]@{
{ $_ } =
{ "Word: $_"; "Length: $($_.Length)" }
{ $_ -match '[aeiou]' } =
{ "Vowels: $($_.ToCharArray() -match '[aeiou]')" }
{ $_ -match '[^aeiou]' } =
{ "Consonant: $($_.ToCharArray() -match '[^aeiou]')" }
})
PowerShell is full of interesting features that are not broadly understood.
WhereFor is built using one of these features, the steppable pipeline.
SteppablePipelines allow you to run one more object pipelines step by step.
WhereFor works in a very simple way. You provide one or more dictionaries or hashtables to WhereFor, and it creates a steppable pipeline for each condition and value.
If the condition returned a value, the action is run.