Let’s say you want to build a command in PowerShell by concatenating a collection of string fragments, and then execute it using the Call
operator:
foreach ($item in $collection) {
$arguments = $arguments + "-someParam $item "
}
& "something.exe " + $arguments
Seems doable, right? Nope. You’ll get this result: error: unknown option <insert value of $arguments here>
What’s happening is the value of $arguments
is effectively being parsed as a single token instead of a series of parameter names followed by argument values. The solution is to create an array of parameter names and arguments instead:
foreach ($item in $collection) {
$arguments = $arguments + @("-someParam", "$item ")
}
& "something.exe " + $arguments