My question is of general nature. Is there a way to determine if a cmdlet is expecting a collection or array in accepting pipeline input?
I understand the general “goto” is the -InputObject parameter if it exists specific to a cmdlet, but what about cmdlets that do not have this parameter?
Would be grateful for any advice, tips, references.
Ignoring pipeline input for a moment, you’ll see it in the help:
-Name [string[]]
The indicates an array is required. If you pass a single value, PowerShell will create a single-item array from it.
But that doesn’t cover pipeline input; pipeline input is always implicitly single items. That is, the pipeline conveys one item at a time from command to command. That allows multiple commands to essentially run simultaneously. Ergo, any parameter capable of accepting pipeline input can “kind of” accept multiple values, but they do it one at a time, not in one big chunk.
Help files clearly indicate which parameters accept pipeline input.
Cmdlets have process blocks that automatically run for every element in an array, as long as the parameter accepts pipeline input. To accept an array directly as a parameter, you would need an extra foreach loop.
# can take an array over the pipe, but not as a parameter, very common
function hi {
param([Parameter(ValueFromPipeline=$True)]$inputvalue)
process {
$inputvalue.gettype()
}
}
PS C:\Users\js> 1,2,3 | hi
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
True True Int32 System.ValueType
True True Int32 System.ValueType
# can take an array as a parameter too, less common
function hi2 {
param([Parameter(ValueFromPipeline=$True)]$inputvalue)
process {
foreach ($value in $inputvalue) {
$value.gettype()
}
}
}
PS C:\Users\js> hi2 -inputvalue 1,2,3
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
True True Int32 System.ValueType
True True Int32 System.ValueType