24

Given:

# test1.ps1
param(
    $x = "",
    $y = ""
)

&echo $x $y

Used like so:

powershell test.ps1

Outputs:

> <blank line>

But then this goes wrong:

test.ps1 -x "Hello, World!" -y "my friend"

Outputs:

Hello,
my

I was expecting to see:

Hello, World! my friend
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
101010
  • 14,866
  • 30
  • 95
  • 172
  • That works from the powershell prompt for me but fails from cmd.exe. Which makes this a cmd.exe limitation. Using single quotes there instead seems to work... which is strange because I didn't think cmd.exe dealt with single quotes at all. – Etan Reisner Feb 04 '15 at 00:09
  • So is this a powershell question or a cmd.exe question? It seems you've answered your own question perhaps? – campbell.rw Feb 04 '15 at 00:16

4 Answers4

41

Well, this is a cmd.exe problem, but there are some ways to solve it.

  1. Use single quotes

    powershell test.ps1 -x 'hello world' -y 'my friend'
    
  2. Use the -file argument

    powershell -file test.ps1 -x "hello world" -y "my friend"
    
  3. 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"
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikolay K
  • 3,770
  • 3
  • 25
  • 37
  • I had this issue with running a PowerShell script from a scheduled task in Windows. One of the parameters of the script was used to build a path and had a space in it, and the script failed because it dropped everything after the space. Adding the `-File` argument fixed the issue. – Lews Therin Aug 27 '18 at 20:30
  • `-File- did not work, I solved it by directly quoting the arg `%1` and escaping the quote which gives: `\"%1\"` . I can then use the `$arg` without any problem (the path doesn't break anymore at the first space) – J. Does Dec 28 '18 at 12:24
  • `-File` helps with a script whose filename contains spaces. However, it affects how PowerShell parses the remaining commandline. An argument `-` produces a weird error (probably a PowerShell bug), as does `-:`. Arguments with parentheses previously had to be put in single quotes; now those have to be removed, otherwise they are passed on to the script. PowerShell 5.1. – Burkart Mar 16 '22 at 19:59
9

One can use a backtick ` to escape spaces:

PS & C:\Program` Files\\....
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Tobin
  • 1,698
  • 15
  • 24
7

A possible solution was in my case to nest the single and the double quotes.

test.ps1 -x '"Hello, World!"' -y '"my friend"'
bubment
  • 73
  • 2
  • 5
  • I was trying to run java application with multiple profiles like `profile1,profile2` and your solution was the only one that worked for me. – Betlista Oct 28 '20 at 21:05
2

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'"
Zach
  • 63
  • 5