$values = {value1, value2, value3...}
Unless value1, etc. are placeholders for numbers (e.g., 42) or strings (e.g., 'foo'), this is invalid syntax, given that { ... } creates a script block, the content of which must be valid PowerShell source code, and the result of which is a piece of PowerShell code meant for later execution on demand, such as with &, the call operator or ., the dot-sourcing operator
In order to create an array, simply use , to separate the elements or enclose them in @(...), the array-subexpression operator:
$values = 'value1', 'value2', 'value3' # , ...
# explicit alternative
$values = @( 'value1', 'value2', 'value3' )
The fact that you're looking to pass multiple -a arguments implies that you're calling an external program:
- PowerShell-native commands do not allow targeting a given parameter multiple times.
When calling external programs, you can pass both parameter names and values as a flat array of string arguments:
# Note: Assumes that `doSomething` is an *external program*
dosomething $values.ForEach({ '-a', $_ })
- Alternatively, you may use
@(...) or - in this case, interchangeably - $(...), the subexpression operator to pass the output from arbitrary commands as individual arguments:
# Note: Assumes that `doSomething` is an *external program*
# Using a pipeline.
dosomething $($values | ForEach-Object { '-a', $_ })
# Using a foreach statement.
dosomething $(foreach ($value in $values) { '-a', $value })
dosomething @(foreach($value in $values) { '-a', $value })