Search code examples
powershell

How to get 'Get-Member' to correctly tell the object type


$array="a", "b", "c"
$array|Get-Member

I am expecting the above to tell me that $array is a array object, but I keep getting TypeName: System.String. I am thinking this is due to get-member checking the last element, rather than the whole object itself. I tried get-memeber $array, but I get an error:

Get-Member: You must specify an object for the Get-Member cmdlet.

am on pwsh 7.4


Solution

  • This is well documented in Example 6: Get members for an array, when the object is piped, the cmdlet returns a member list for each unique object type in the array. If you pass the array using the -InputObject parameter, the array is treated as a single object.

    The reason for the error is due to the parameter not being set up for positional binding, in other words the Parameter decoration didn't specify a Position. Worth adding, for binary cmdlets, this is a mandatory requirement whereas for PowerShell functions, the position can be inferred unless explicitly disabled via PositionalBinding set to false.

    Get-Member -InputObject $array