27

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
2
  • 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. Commented Feb 4, 2015 at 0:09
  • So is this a powershell question or a cmd.exe question? It seems you've answered your own question perhaps? Commented Feb 4, 2015 at 0:16

4 Answers 4

46

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"
    
Sign up to request clarification or add additional context in comments.

3 Comments

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.
-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)
-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.
10

One can use a backtick ` to escape spaces:

PS & C:\Program` Files\\....

1 Comment

Or as mentioned above escape the quotes in the command line, e.g. powershell somefile \"arg with space\"
8

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

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

1 Comment

I was trying to run java application with multiple profiles like profile1,profile2 and your solution was the only one that worked for me.
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'"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.