Forum Discussion

jfkrueger's avatar
jfkrueger
Copper Contributor
Oct 11, 2024

Run Start-Process with -ArgumentList when an argument also contains a -a

I'm trying to run this command: 

Start-Process C:\ProgramData\GrapeCity\gclm\gclm.exe -ArgumentList '"product id" -a "key"'

But getting the error: Cannot bind parameter because parameter 'ArgumentList' is specified more than once.

I'm guessing this is due to the -a in my actual argument list. How can I run this command?

 

The original command outside of powershell looks like this: C:\ProgramData\GrapeCity\gclm\gclm.exe "product id" -a "key"

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    jfkrueger 

     

    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

    • jfkrueger's avatar
      jfkrueger
      Copper Contributor
      Thank you so much for the reply, what you say does make sense. I did try your last example and I get the same error:

      Run powershell Start-Process -FilePath "$env:PROGRAMDATA\GrapeCity\gclm\gclm.exe" -ArgumentList @("product-id", '-a "License-Key"');

      Start-Process : Cannot bind parameter because parameter 'ArgumentList' is specified more than once. To provide
      multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter
      value1,value2,value3".
      At line:1 char:115
      + ... clm.exe -ArgumentList 742a5cf8-2204-4873-9dc9-ff56d67411bd -a "065042 ...
      + ~~
      + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
      + FullyQualifiedErrorId : ParameterAlreadyBound,Microsoft.PowerShell.Commands.StartProcessCommand

      Error: Process completed with exit code 1.
      • jfkrueger's avatar
        jfkrueger
        Copper Contributor

        From looking at the error message, I tried this command (wrapping the -ArgumentList in quotes): 

         

        powershell Start-Process -FilePath "$env:PROGRAMDATA\GrapeCity\gclm\gclm.exe" "-ArgumentList 'product-id', '-a licensekey'";

         

        And it works!

         

        Thank you for your help!

Resources