1

I want to execute an external program like so

& $exe $arguments

$arguments is string with spaces, lets say its a b c d

What happens is that the argument passed to the exe is "a b c d"

with double quotes around the whole thing. It does not happen when $arguments contains no spaces.

How can I prevent powershell from trying to be smart and stop it from wrapping my string in double quotes? This is a ridiculous to assume that everything with spaces must be a path.

EDIT: Since a fix apparantly does not exist, I did work around by converting every bit into an array, and it does work in PS 5. However PS4 is being ****...

What I want is to have a command line argument which looks like -filter:"+[RE*]* +[RR*]*"

PS 4 puts double quotes around the whole thing too: $filter = "+[RE*]*", "+[RR*]*" & $opencover -target:"$xunit" "-targetargs:$allPaths $traitsCmd -quiet -nunit xunit-$results -noshadow" -register:user -filter:"$filter" -output:opencover-$results

If I replace -filter: with '-filter:' I end up with a space between -filter: and the contents of the $filter array. Whatever I do I can't get rid of it, without wrapping the whole thing in doublequotes like "-filter:..."

Lev
  • 309
  • 1
  • 12
  • have you tried declaring `$arguments` with single quotes like this: `$arguments = 'a', 'b', 'c', 'd'`? If not try that – Paul Sep 07 '16 at 10:49
  • a b c d are there to illustrate the problem. In my script those are much more complicated and created programmatically – Lev Sep 07 '16 at 11:01
  • It does not, because I do not have string literals. I have expressions, they do not expand inside single quotes – Lev Sep 07 '16 at 12:28
  • thanks I''l try that – Lev Sep 07 '16 at 13:27
  • didn't work in PS4. It does work alone, but if the whole thing is an argument, PS4 wraps it in double-quotes – Lev Sep 07 '16 at 13:57
  • not sure what you mean tbh. are you trying `$arguments = ' a b c d'` now or `$arguments = 'a','b','c'..` or something entirely else? if you want we can move this to chat and hopefully find a solution without cluttering the comments any more – Paul Sep 07 '16 at 14:01
  • There's a function in [this article](http://windowsitpro.com/powershell/running-executables-powershell) that may be of use to you. – Bill_Stewart Sep 07 '16 at 14:02
  • `& $exe '--%' $arguments` – user4003407 Sep 07 '16 at 14:27

4 Answers4

3

You should make an array instead of trying. So in your case:

$exe = "ping.exe"
$arguments = "-t","8.8.8.8"
& $exe $arguments

Please note that $args is a special value and cannot be assigned to anything. That is why I'm using $arguments in my example.

The answer to your question is then this:

$arguments = "a","b","c","d"
& $exe $arguments
Jonas Andersen
  • 489
  • 4
  • 12
  • I had that. The problem is, even when an array element contains spaces. The powershell wraps it in double quotes. I just need to stop powershell from being pseudo-smart. – Lev Sep 07 '16 at 09:44
0

Split your space-separated string into an array:

$arguments = "a b c d"
$arguments = $arguments -split '\.'
& $exe $arguments
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

I strongly recommend using Start-Process instead of using the . and & call operators, for the purpose of calling external executables from PowerShell.

The Start-Process command is much more predictable, because you are feeding in the path to the executable and command line arguments separately. There isn't any special interpretation (aka. "magic") going on that way.

$MyProcess = Start-Process -FilePath $exe -ArgumentList $arguments -Wait

I've shared similar advice on several other StackOverflow threads.

Powershell "Start-process" not running as scheduled task

Accessing output from Start-Process with -Credential parameter

Community
  • 1
  • 1
  • The "magic" is pretty straightforward once you know the basic rules. Also, I wrote the showargs.exe utility to show you exactly what PowerShell is doing (no more parsing mysteries). It's all described in [an article](http://windowsitpro.com/powershell/running-executables-powershell) I wrote a while back. – Bill_Stewart Sep 07 '16 at 18:53
0

When passing complex arguments to an exe, consider using the stop parsing operator: --%. From that operator to the end of the line, PowerShell parsing "features" go away and the text is parsed very similarly to how CMD would parse the text. In fact, the only way to reference variables is the CMD way %var% e.g:

77> $env:arguments = 'a b c d'
78> echoargs --% %arguments%
Arg 0 is <a>
Arg 1 is <b>
Arg 2 is <c>
Arg 3 is <d>

Command line:
"C:\Users\Keith\Documents\WindowsPowerShell\Modules\Pscx\3.2.2\Apps\EchoArgs.exe"  a b c d
Keith Hill
  • 194,368
  • 42
  • 353
  • 369