Home Dashboard Directory Help
Search

Where-Object simplified syntax needs way to access pipeline object as well as properties by Keith Hill MVP


Status: 

Active


7
0
Sign in
to vote
Type: Suggestion
ID: 793887
Opened: 7/14/2013 12:27:39 PM
Access Restriction: Public
0
Workaround(s)
view

Description


There are times that I'm using the simplified syntax for Where-Object and I need to compare against the actual pipeline object and not one of its properties. We need some moniker to represent the pipeline object e.g.:

'a','b','c' | where _ -eq b

and/or

'a','b','c' | where PSItem -eq b
Details
Sign in to post a comment.
Posted by Derp McDerp on 12/3/2013 at 10:50 PM
There is an easier way to fix this. Here is how the Where-Object cmdlet is currently implemented:


Parameter Set: EqualSet
Where-Object [-Property] <String> [[-Value] <Object> ] [-EQ] [-InputObject <PSObject> ] [ <CommonParameters>]

Parameter Set: CaseSensitiveContainsSet
Where-Object [-Property] <String> [[-Value] <Object> ] -CContains [-InputObject <PSObject> ] [ <CommonParameters>]

...

Parameter Set: ScriptBlockSet
Where-Object [-FilterScript] <ScriptBlock> [-InputObject <PSObject> ] [ <CommonParameters>]



What the PS team can do instead is to modify the cmdlet to look like:


Parameter Set: SelectionSet
Where-Object [-Property] <String> [-InputObject <PSObject> ] [ <CommonParameters>]

Parameter Set: EqualSet
Where-Object [[-Property] <String>] -EQ <Object> [-InputObject <PSObject> ] [ <CommonParameters>]

Parameter Set: CaseSensitiveContainsSet
Where-Object [[-Property] <String>] -CContains <Object> [-InputObject <PSObject> ] [ <CommonParameters>]

...

Parameter Set: ScriptBlockSet
Where-Object [-FilterScript] <ScriptBlock> [-InputObject <PSObject> ] [ <CommonParameters>]




i.e.

1. Add a new ParameterSet called "SelectionSet"
2. Change every parameter set (except SelectionSet) from "[-Property] <String>" to "[[-Property] <String>]"
2. Change every parameter set from "[[-Value] <Object>] -OP" to "-OP <String>"
4. If -Property is unbound, use $InputObject as the operand instead of $InputObject.$Property




With these changes, your example now becomes even simpler:

write a b c |? -eq b
Sign in to post a workaround.