Forum Discussion
Run Start-Process with -ArgumentList when an argument also contains a -a
Hi, Joe.
You are correct that "-a" is being interpreted as "-ArgumentList" and therefore considered as the second instance of -ArgumentList within that command. Confirmation can be found here:
If you look at the -ArgumentList parameter definition above, ArgumentList is meant to be an array of strings, which could be expressed as either of the following:
# Strictly correct.
Start-Process "somepath\glcm.exe" -ArgumentList @("productId", '-a "key"');
# "Shorthand" (barely).
Start-Process "somepath\glcm.exe" -ArgumentList "productId", '-a "key"';
However, if you ignore the comma separating the array elements then it is not an array at all. In such a scenario, PowerShell interprets whatever values beyond the first as parameters belonging to the calling commandlet, and that's where "-a" pattern matches "-ArgumentList".
To avoid this, use commas to separate the parameters as well as enclosing single parameters that contain spaces (such as '-a "key"') in quotes (note that double and single quotes have different behaviours in PowerShell, though I won't go into that here) as shown in the above examples.
By using the comma separator, PowerShell will treat all "joined" values as part of the ArgumentList string array.
I would also encourage you to use named parameters rather than positional parameters, as the latter is more prone to breaking between platforms or when the commandlet itself is updated in such a way that the parameter ordering changes. While this is incredibly rare, it's also entirely avoidable for practically no extra effort.
For example:
# Using named parameters instead of positional parameters, along with strict parameter specification.
Start-Process -FilePath "somepath\glcm.exe" -ArgumentList @("productId", '-a "key"');
Cheers,
Lain