17

For whatever reason, when I try to call a C# program I'm writing, and I try to pass two arguments with '--' in the command line, PowerShell doesn't call the program with my command line.

For instance, I'm providing the command line:

.\abc.exe foo.txt -- bar --

When I call this, the C# program's main only gets the command line arguments:

foo.txt bar --

instead of

foo.txt -- bar --

as would be expected.

Why would this be happening?

BTW, if I call it as:

.\abc.exe foo.txt '--' bar '--'

it works as expected.

Also, calling it as:

& .\abc.exe foo.txt -- bar --

Doesn't seem to help.

My reason for thinking this is a PowerShell weirdness is that if I run the same command line from CMD.EXE, everything works as expected.

1
  • a further note. apparently the two '--' isn't the whole problem. It seems that powershell dropbx the first '--' even if the second one isn't there. Seems like -- must have special meaning for powershell. Commented Apr 3, 2013 at 6:27

2 Answers 2

17

A double hyphen instructs PowerShell to treat everything coming after as literal arguments rather than options, so that you can pass for instance a literal -foo to your script/application/cmdlet.

Example:

PS C:\> echo "-bar" | select-string -bar
Select-String : A parameter cannot be found that matches parameter name 'bar'.
At line:1 char:28
+ "-bar" | select-string -bar <<<<
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand

vs.

PS C:\> echo "-bar" | select-string -- -bar

-bar

To avoid this behavior you must either quote ("--", '--') or escape (`--) the double hyphen.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you give a reference to doc somewhere that describes the purpose of '--' and how it instructs PS to do this?
This answer from @ravikanth to a similar question cites "Windows PowerShell in Action", but other than that I don't have a source. It's consistent with other shells, though (bash for instance).
Here's a source for running executables in PowerShell that's well worth looking at: social.technet.microsoft.com/wiki/contents/articles/…
@JamieSee I don't see the double-dash discussed in that article, only the "stop parsing" parameter (--%) that was introduced with PowerShell v3.
I anyone is looking for the official docs about this -- feature, here it is.
10

With PowerShell 3 you can use --% to stop the normal parsing PowerShell does.

.\abc.exe --% foo.txt -- bar --

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.