The help for Split-Path
states that the parameters -LiteralPath
and -Leaf
are incompatible:
SYNTAX
Split-Path [-Path] <System.String[]> -Leaf [-Resolve] # ...
Split-Path -LiteralPath <System.String[]> [-Resolve] # ...
This command failing confirms it:
Split-Path -LiteralPath 'C:\foo.txt' -Leaf
# Split-Path: Parameter set cannot be resolved using the specified named parameters.
# One or more parameters issued cannot be used together
And yet, piping works just fine:
Get-Item 'C:\foo.txt' | Split-Path -Leaf
Why is that?
I thought Split-Path
would receive an object from the pipeline and try to bind that object's PSPath
property to its -LiteralPath
parameter (alias: PSPath
), as explained here.
I imagine that because I supplied the -Leaf
switch, the parameter binder knew to use the parameter set containing -Leaf
, which took -LiteralPath
out of the picture. But then how does PSPath
end up bound to -Path
? Does the binder automatically call .ToString()
on the object to obtain the path as a string, which it can then bind to -Path
by value instead of by
property name? How does it know to do that?
I tried using Trace-Command, but I'm unable to understand the output.
Trace-Command ParameterBinding { Get-Item 'C:\foo.txt' | Split-Path -Leaf } -PSHost