0

My question pertains more toward PowerShell, but for completeness I am using AutoHotKey to run the PowerShell command.

I'm trying to pass some arguments to PowerShell with the "-Command" parameter, but running into issues if the arguments contain "special" characters.

For example, I have three folders:

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

I am also testing both PowerShell 5.1 (built-in to Windows 10) and new PowerShell 7.0.1 (portable version) with Windows Terminal (wt.exe). These are the commands I have tried using 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%'"

Either usage of PowerShell works with folders that don't contain special characters.

With a folder that has an apostrophe (seen as a single quote) in the name, such as c:\folder doesn't work, PowerShell 5.1 throws the following error:

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

PowerShell 7.0.1 doesn't even throw an error. It doesn't show anything really.

With a folder that has square brackets "[]", both PowerShell 5.1 and 7.0.1 don't show anything either. Not even an error.

I think I have an issue with escaping the characters or quoting it properly.

I would really appreciate any input on how I can get my code to work.

EDIT: Forgot to mention, I am using Windows Terminal (wt.exe) for PowerShell 7.0.1.

howdoicode
  • 779
  • 8
  • 16
  • 1) Use `-LiteralPath` instead of `-Path` and 2) use (doubly escaped) double-quotes instead of single-quotes around the argument – Mathias R. Jessen May 25 '20 at 14:42
  • @MathiasR.Jessen Using `-LiteralPath` and doubly escaped double-quotes, it works under PowerShell 5.1, but not under PowerShell 7.0.1. – howdoicode May 25 '20 at 14:55
  • Does it throw the same error? (missing string terminator) – Mathias R. Jessen May 25 '20 at 15:02
  • @MathiasR.Jessen Forgot to mention I am using Windows Terminal in the command (`Run, wt.exe "c:\ps7\pwsh.exe" -NoExit -ExecutionPolicy Bypass -Command "Get-ChildItem -LiteralPath ""%Clipboard%"""`). Using PowerShell 7.0.1, it throws the error `Get-ChildItem: A positional parameter cannot be found that accepts argument 'works'.` for `c:\folder that works`, and `Get-ChildItem: A positional parameter cannot be found that accepts argument 'not'.` for`c:\[01] folder not working either`. Nothing is displayed for `c:\folder doesn't work`. **Without** `wt.exe`, PowerShell 7.0.1 works. – howdoicode May 25 '20 at 15:18
  • This is tough.. – js2010 May 25 '20 at 16:58

2 Answers2

0

This works in cmd at least. If the directory is empty, it won't show anything.

powershell dir -literal (get-clipboard)
pwsh -c dir -literal (get-clipboard)
js2010
  • 23,033
  • 6
  • 64
  • 66
0

With some experimentation, I have a partial answer to my own problem. Both PowerShell 5.1 and 7.0.1, by themselves (without the use of Windows Terminal), work when using the following commands in 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%`"`"`" "

For some reason, I had to escape the quotes surrounding the AutoHotKey's %Clipboard%, three times using `" (backtick/grave and double quote)

But once I added Windows Terminal (wt.exe) to AutoHotKey's Run command, it would fail on all three folders in some way. I think I'll create a separate question just for Windows Terminal.

howdoicode
  • 779
  • 8
  • 16