How do I pass in a string with spaces into PowerShell?
One can use a backtick ` to escape spaces:
PS & C:\Program` Files\\....
Well, this is a cmd.exe
problem, but there are some ways to solve it.
Use single quotes
powershell test.ps1 -x 'hello world' -y 'my friend'
Use the
-file
argumentpowershell -file test.ps1 -x "hello world" -y "my friend"
Create a
.bat
wrapper with the following content@rem test.bat @powershell -file test.ps1 %1 %2 %3 %4
And then call it:
test.bat -x "hello world" -y "my friend"
A possible solution was in my case to nest the single and the double quotes.
test.ps1 -x '"Hello, World!"' -y '"my friend"'
I had a similar problem but in my case I was trying to run a cmdlet, and the call was being made within a Cake script. In this case the single quotes and -file
argument did not work:
powershell Get-AuthenticodeSignature 'filename with spaces.dll'
Resulting error: Get-AuthenticodeSignature : A positional parameter cannot be found that accepts argument 'with'.
I wanted to avoid the batch file if possible.
Solution
What did work was to use a cmd wrapper with /S to unwrap outer quotes:
cmd /S /C "powershell Get-AuthenticodeSignature 'filename with spaces.dll'"