转义 -LiteralPath

Ash*_*Ash 4 registry powershell escaping

我通过设置将 PowerShell 添加到我的上下文菜单中:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Powershell\command]
@="powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
Run Code Online (Sandbox Code Playgroud)

这很有效,除了我巧妙地命名为Ash's docs. 哪个失败了:

该字符串缺少终止符:'。
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullQualifiedErrorId : TerminatorExpectedAtEndOfString

Set-Location文档:

指定位置的路径。LiteralPath参数的值与键入的值完全相同。没有字符被解释为通配符。如果路径包含转义字符,请将其括在单引号中。单引号告诉 PowerShell 不要将任何字符解释为转义序列。

我是否可以在这里使用-Path并希望我的目录中没有可能包含通配符的目录?或者可以逃脱 中的单引号%V吗?

Ans*_*ers 5

将路径放在双引号中而不是单引号中。双引号不能出现在路径中。但是,双引号必须转义两次:对于 .reg 文件和 PowerShell 调用。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Powershell\command]
@="powershell.exe -NoExit -Command Set-Location -LiteralPath \\\"%V\\\""


Run Code Online (Sandbox Code Playgroud)

  • @Ash 我在发布之前对此进行了测试,所以我严重怀疑这不起作用,除非你做错了什么。 (2认同)