Is it possible to convert following where PS script block
$env:Path -split ";" | where {$_ -like "c:\Program*"}
to a where comparison statement looking like
$env:Path -split ";" | where ???WHAT HERE??? -like "c:\Program*"
What I need is something that would replace the implicit $_ variable from the first statment.
Edit: My intention is pure curiosity. I prefer to use the second way of the where-object cmdlet, however, I do not know how to address the pipe value directly on the left side of the -like operator
Edit2: What is a comparison statement? A comparison statement is much more like natural language. Comparison statements were introduced in Windows PowerShell 3.0. An example:
Get-Process | Where-Object PriorityClass -eq "Normal"
Source: Where-Object on MSDN
Is it possible to convert following where PS script block
$env:Path -split ";" | where {$_ -like "c:\Program*"}
to a where comparison statement looking like
$env:Path -split ";" | where ???WHAT HERE??? -like "c:\Program*"
No, that is not possible.
There are two modes for Where-Object
. Fully expressed, one looks like this:
ls | Where-Object -FilterScript { $_.Name -eq 'File.txt' }
Here, we're writing a scriptblock that contains an expression that is evaluated on every item.
The other mode, fully expressed, actually looks like this.
ls | Where-Object -Property Name -Value File.txt -Eq
Note that the -Eq
here is a parameter and not an operator. We're calling a command, not writing an expression in a scriptblock here. And I know what you're thinking. "But to work that way, the command would need to have a different parameter set for every conceivable operator!" And that's exactly what they do!
Where-Object [-InputObject <PSObject>] [-Property] <String> [[-Value] <Object>] [-EQ] [<CommonParameters>]
Where-Object [-InputObject <PSObject>] [-Property] <String> [[-Value] <Object>] [-NE] [<CommonParameters>]
Where-Object [-InputObject <PSObject>] [-Property] <String> [[-Value] <Object>] [-LT] [<CommonParameters>]
[...]
The -InputObject
parameter is the value from the pipeline. -Property
is the first positional parameter. -Value
is the second positional parameter.
The problem is that the -Property
parameter is mandatory. You can't call this command without specifying a parameter name and there's no property name that means "this object".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With