Is there a function in PowerShell for escaping characters in paths?
NB: I'm aware that most cmdlets which provide a Path
parameter also provide a LiteralPath
parameter which resolves this issue. This question is more from curiosity than need, as in most use cases I can think of, switching to LiteralPath
makes sense. However there are some genuine use-cases (e.g. Start-BitsTransfer
has a Source
parameter, but no literal equivalent).
If I have a file c:\temp\test[0123].txt
, instead of Get-Item 'c:\temp\test[0123].txt'
, I'd have to use Get-Item 'c:\temp\test`[0123`].txt'
to get a result (or make use of the LiteralPath
parameter).
Even the path returned by another PowerShell command returns an unescaped string; i.e. Get-ChildItem 'c:\temp\' -Filter 'test*.txt' | Convert-Path | Get-Item
fails (NB: if we pass the actual FileSystemInfo
object all works, but that object has no properties with the correctly escaped string path).
We can easily escape this using code such as below:
$path = 'c:\temp\test[0123].txt'
$path = $path -replace '([][[])', '`$1' # escape square brackets by adding back ticks
Get-Item $path
However when escaping strings, standard advice is to avoid rolling your own solution / to make use of the language's solutions to these issues.
Is there any pre-existing function in PowerShell for this purpose, or any recommended way of approaching this; or is the only option to use a bespoke function (e.g. below)?
function ConvertFrom-LiteralPath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$LiteralPath
)
process {
(New-Object -TypeName 'PSObject' -Property @{
LiteralPath = $LiteralPath
Path = $LiteralPath -replace '([][[\*\?])', '`$1' # escapes ], [, *, and ?
})
}
}
Info on special characters:
To search for a special character that has a special function in the query syntax, you must escape the special character by adding a backslash before it, for example: To search for the string "where?", escape the question mark as follows: "where\?"
To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.
Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped.
There is the following method you can use:
[Management.Automation.WildcardPattern]::Escape('test[1].txt')
Returns:
test`[1`].txt
Documented here:
https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.wildcardpattern.escape
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