1

If I have a pwsh script like:

param (
  [string[]]$inDirs
)
foreach ($dir in $inDirs){
  write-host $dir
}

and I do

pwsh myscript.ps1 'c:\\some\dir\'

The result is

c:\\some\dir"

Where somehow, the last backslash + single quote has been turned into a double quote. This walks like a bug and stinks like a bug, particularly as the other backslashes don't escape anything, just the one next to the single quote, and that the "escaped" character isn't one that is in the original string, but apparently it's as-designed.

Obviously I can call the script and take care not to add a trailing slash, but guess what happens if I use tab-completion to supply the name of a directory? "Hi, I'm clippy, it seems like you're tab-completing a directory path, I'll just go ahead and add a good ol' trailing slash for you there".

Tested with pwsh 7.5.2 on Windows Server 2019 Datacenter

7
  • powershell version? Commented Jul 4 at 4:39
  • PowerShell 7.5.2 Commented Jul 4 at 5:01
  • same version but I am unable to replicate the issue Commented Jul 4 at 8:03
  • 2
    Ok, so to reproduce you need to use powershell.exe as the host and the path needs to have a space in it - (i) start a powershell.exe interactive session (ii) run pwsh myscript.ps1 'c:\\some\ dir\' (note the space before dir\ (iii) the script outputs c:\\some\ dir". Commented Jul 4 at 8:29
  • 2
    Welcome to the quoting hell. I guess this is a duplicate with How to solve problem with quotes in arguments on calling a PowerShell script with -File option?. Workaround: pwsh myscript.ps1 "'c:\\some\ dir\'" Commented Jul 4 at 8:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.