15

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kelly L
  • 366
  • 1
  • 3
  • 8
  • 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. – Kelly L Apr 03 '13 at 06:27

2 Answers2

13

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Can you give a reference to doc somewhere that describes the purpose of '--' and how it instructs PS to do this? – Kelly L Jan 28 '14 at 00:46
  • [This answer](http://stackoverflow.com/a/12198146/1630171) 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). – Ansgar Wiechers Jan 28 '14 at 09:35
  • Here's a source for running executables in PowerShell that's well worth looking at: http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx – JamieSee May 16 '16 at 21:48
  • @JamieSee I don't see the double-dash discussed in that article, only the "stop parsing" parameter (`--%`) that was introduced with PowerShell v3. – Ansgar Wiechers May 16 '16 at 22:17
  • I anyone is looking for the official docs about this `--` feature, [here](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.3#the-end-of-parameters-token) it is. – Mariusz Pawelski May 05 '23 at 21:53
9

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

.\abc.exe --% foo.txt -- bar --
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143