1

I don't understand why in some cases it's ok to have spaces in parameters and in some cases it's not.

In this example it's ok to have spaces in $file variable, but not in $path.

$file = "test file.tar"
$path = "a b\"
New-Item -Path $path -Type Directory
# Make an empty tar file
tar -cf $file -T NUL
# extract it to $path
tar -xf $file -C $path

Gives you

tar.exe: could not chdir to 'a b"'

And most bizarre

tar -xf $file -C `"$path

works just fine.

I'm on Windows 11 and using the default PowerShell 5.1.

1
  • 1
    You're seeing a Windows PowerShell bug that has since been fixed in PowerShell (Core) 7+: a string with spaces that ends in \ is incorrectly passed to external programs in that the necessary escaping as \\ in the - of necessity - double-quoted string that is used behind the scenes isn't performed. See the linked duplicate for details. Commented Apr 20, 2023 at 15:53

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.