Lev
Lev

Reputation: 309

Stop powershell from putting doublequotes around arguments

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:..."

Upvotes: 1

Views: 719

Answers (4)

Keith Hill
Keith Hill

Reputation: 201602

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

Upvotes: 0

user189198
user189198

Reputation:

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

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

Split your space-separated string into an array:

$arguments = "a b c d"
$arguments = $arguments -split '\.'
& $exe $arguments

Upvotes: 0

Jonas Andersen
Jonas Andersen

Reputation: 489

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

Upvotes: 3

Related Questions