Add a parameter like -InterpretPipeLiterally
such that
$Values | Convert-Path -InterpretPipeLiterally
is equivalent to
```
$Values | ForEach-Object {Convert-Path -LiteralPath $_}
You can do this with scriptblock binding syntax 馃檪
$Values | Convert-Path -LiteralPath { $_ }
Oooh, wow, didn't know that! Thanks
Wait. Where is the documentation for this? I can't seem to get it to work.
@vexx32
Searching "scriptblock binding syntax" only leads to scriptblock documentation
Documentation is here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7#using-delay-bind-script-blocks-with-parameters
Yeah, it's a bit confusing that it's buried in the scriptblock article, but there's not really a much better place to put it, it's a general purpose syntax available to all commands.
If you can share the code you're running into issues with, I can try to point out what's going wrong. The important limitations with the scriptblock binding for parameters are:
[scriptblock]
-type parametersGet-Help <command> -Parameter <parameter>
results to see if a given parameter supports pipeline input.Assuming I have a parameter that accepts any Object
, if I want to pass a ScriptBlock
, I can just write ({鈥)
, right?
The issue I ran into is that this apparently requires specifying an explicit parameter. I was doing Get-PSDrive | Set-Location {"${_}:"}
as an experiment. Get-PSDrive | Set-Location -Path {"${_}:"}
worked.
Ah yeah, it has to be a specific named parameter. I don't know the full implementation details but yeah, that's a hard requirement.
Also, [object]
params will just accept a scriptblock as-is. They never invoke pipeline binding.
Probably not for your specific use case, but you can also construct the objects with property names, for example:
[pscustomobject]@{ LiteralPath= "c:\"} | Convert-Path
Does require the cmdlet supports ValueFromPipelineByPropertyName, but they typically do when they have distinct inputs with the same underlying type
Yep! Very often (if not always) -LiteralPath
parameters also support a PSPath
alias both directly and on the property, because providers' objects in PS have a PSPath property that they'll use when piped into a provider cmdlet. 馃檪
Most helpful comment
You can do this with scriptblock binding syntax 馃檪