使用 AutoHotKey 和 Windows 终端将参数传递给带有“-Command”参数的 PowerShell

Passing arguments to PowerShell with "-Command" parameter using AutoHotKey and Windows Terminal

我的问题更多地与 PowerShell 有关,但为了完整起见,我使用 AutoHotKey 来 运行 PowerShell 命令。

我正在尝试使用“-Command”参数将一些参数传递给 PowerShell,但是如果参数包含 "special" 个字符,运行就会出现问题。

比如我有三个文件夹:

c:\folder that works
c:\folder doesn't work
c:\[01] folder not working either

我还在使用 Windows 终端 (wt.exe) 测试 PowerShell 5.1(内置于 Windows 10)和新的 PowerShell 7.0.1(便携式版本)。这些是我使用 AutoHotKey 尝试过的命令:

Run, powershell.exe -NoExit -ExecutionPolicy Bypass -Command "Get-ChildItem -Path '%Clipboard%'"
Run, wt.exe "c:\ps7\pwsh.exe" -NoExit -ExecutionPolicy Bypass -Command "Get-ChildItem -Path '%Clipboard%'"

PowerShell 的两种用法都适用于不包含特殊字符的文件夹。

对于名称中包含撇号(视为单引号)的文件夹,例如 c:\folder doesn't work,PowerShell 5.1 会引发以下错误:

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

PowerShell 7.0.1 甚至不会抛出错误。它并没有真正显示任何东西。

对于带有方括号“[]”的文件夹,PowerShell 5.1 和 7.0.1 也不显示任何内容。甚至没有错误。

我想我在转义字符或正确引用它时遇到问题。

我非常感谢任何关于如何让我的代码工作的意见。

编辑: 忘了说,我正在使用 Windows 终端 (wt.exe) for PowerShell 7.0 .1.

这至少在 cmd 中有效。如果目录为空,则不会显示任何内容。

powershell dir -literal (get-clipboard)
pwsh -c dir -literal (get-clipboard)

通过一些实验,我对自己的问题有了部分答案。 PowerShell 5.1 和 7.0.1 本身( 不使用 Windows 终端 )在 AutoHotKey 中使用以下命令时工作:

Run, powershell.exe -NoExit -ExecutionPolicy Bypass -Command " Get-ChildItem -LiteralPath `"`"`"%Clipboard%`"`"`" "
Run, "c:\ps7\pwsh.exe" -NoExit -ExecutionPolicy Bypass -Command " Get-ChildItem -LiteralPath `"`"`"%Clipboard%`"`"`" "

出于某种原因,我不得不使用 `"(backtick/grave 和双引号)

三次转义 AutoHotKey %Clipboard% 周围的引号

但是一旦我将 Windows 终端 (wt.exe) 添加到 AutoHotKey 的 Run 命令中,它将以某种方式在所有三个文件夹上失败。我想我会为 Windows 终端创建一个单独的问题。