im_chc
im_chc

Reputation: 1075

Powershell: pipe a multi-line string to resolve-path's -LiteralPath

Using Powershell, I want to convert a bunch of absolute paths to relative paths:

cmd /c dir /s /b /a-d | resolve-path -relative | sort-object

However, there are many funny characters in the directories that confuses Resolve-Path that it thought they were wildcard patterns.

How to pipe them to resolve-path's -LiteralPath? I read that it should be done by ByPropertyName piping and that the value should be put to the "LiteralPath" property of the input object.

But I don't know how. The articles on the web confuse the hell out of me.

Please help and many thanks.

Upvotes: 2

Views: 525

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174824

Specify that the pipeline input value should be bound to LiteralPath instead, like this:

cmd /c dir /s /b /a-d|Resolve-Path -LiteralPath {$_} -Relative

Although, if you just want to recurse through the directory tree, gather all files and resolve their relative path, use Get-ChildItem instead - the LiteralPath parameter on built-in cmdlets is always aliased PSPath, which happens to be the name of a property that the providers add to all drive items, making the pipeline-binding work automatically:

Get-ChildItem -File -Recurse |Resolve-Path -Relative 
                                           # Explicit binding to LiteralPath no longer required

Upvotes: 4

Related Questions