How can I determine if the InputObject is a collection or array?

Welcome Forums General PowerShell Q&A How can I determine if the InputObject is a collection or array?

This topic contains 6 replies, has 4 voices, and was last updated by

js
 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #108947

    Participant
    Points: 109
    Rank: Participant

    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.

     

  • #108952

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    Input datatype for a parameter is same even if it accepts input via pipeline.

    You can check the input datatype and more for all parameters in a cmdlet using

    Get-Help Get-Process -Full

    If your target is specific to parameter, you can get it via

    Get-Help Get-Process -Parameter Name
  • #108979

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    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.

    • #109006

      Participant
      Points: 109
      Rank: Participant

      Thank you Mr Jones, much appreciated.

  • #109024
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    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
    • #109870

      Participant
      Points: 109
      Rank: Participant

      Many thanks Mr JS, much appreciated.

      Your two examples have indeed clarified many of my wrong impressions with pipeline processing.

      Best,

  • #109873
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    Thanks. Sometimes I get ghosted after answering, lol.

You must be logged in to reply to this topic.