1

I was wondering if it was possible to call a parameter twice within a cmdlet, such as:

cmdlet-test -myCommand input1, input2 -myCommand input3, input 4

myCommand being the same command twice. Is that possible or will a user just have to write it all in one comma separated list?

3
  • Why is specifying the parameter more than once is preferable over using a single comma-delimited list or other collection? From the phrase "will a user just have to write it all in one comma separated list", I take it that this is not a desirable method of running the cmdlet for you. Commented May 30, 2014 at 15:12
  • it's just the way that was foreseen to get the job done, but if it isn't possible then it'll have to be done with a comma separated list. I was just curious if there was another way to do it. Commented May 30, 2014 at 15:25
  • The idea of reusing a parameter in the way you describe is foreign to every programming language I'm familiar with. Commented May 30, 2014 at 16:40

2 Answers 2

1

If the user attempts to use the same parameter twice, they will receive an error.

get-process | select -first 2

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    201      16     3324       3456   109     0.17   9972 acrotray
    409      16     3904       4948   101     0.97  10520 AdobeARM

Let's try it the conventional way:

get-process -pid 9972,10520

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    201      16     3324       3456   109     0.17   9972 acrotray
    409      16     3904       4948   101     0.97  10520 AdobeARM

And your proposed method:

get-process -pid 9972 -pid 10520
Get-Process : Cannot bind parameter because parameter 'Id' 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:23
+ get-process -pid 9972 -pid 10520
+                       ~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Process], ParameterBindingException
    + FullyQualifiedErrorId : ParameterAlreadyBound,Microsoft.PowerShell.Commands.GetProcessCommand
Sign up to request clarification or add additional context in comments.

Comments

0

For those who would like to separate a really long function argument into several lines, I suggest this approach:

# Just an example function
function PrintFileName {
  param (
    [Parameter(Mandatory = $true)] [System.String[]] $Files = @()
  )

  foreach ($File in $Files) {
     Write-Host "${File}"
  }
}

Then, the function call like this:

PrintFileName -Files C:\SomeDirectory\SomeFile.txt1,C:\SomeDirectory\SomeFile2.txt,`
C:\SomeDirectory\SomeFile2.txt,C:\SomeDirectory\SomeFile3.txt,`
C:\SomeDirectory\SomeFile4.txt,C:\SomeDirectory\SomeFile5.txt

Will output:

C:\SomeDirectory\SomeFile1.txt
C:\SomeDirectory\SomeFile2.txt
C:\SomeDirectory\SomeFile2.txt
C:\SomeDirectory\SomeFile3.txt
C:\SomeDirectory\SomeFile4.txt
C:\SomeDirectory\SomeFile5.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.