Reputation: 585
In powershell, if a object returns multiple values for one element, then I can't figure out how to separate those values.
So, for example:
'1' | %{@("ValueC_$_.1","ValueC_$_.2","ValueC_$_.3")[0]}
Returns ValueC_1.1 as I would expect. But..
$Object = [System.Collections.ArrayList]@()
(1..4) | %{
$null = $object.Add([pscustomobject] @{
"PropertyA"="ValueA_$_"
"PropertyB"="ValueB_$_"
"PropertyC"=@("ValueC_$_.1","ValueC_$_.2","ValueC_$_.3")
})
}
$Object | Select-Object PropertyC -First 1 | %{$_[0]}
Returns this instead:
{ValueC_1.1, ValueC_1.2, ValueC_1.3}
So, how do I separate each of those subvalues and specify just the first value of each object?
Upvotes: 3
Views: 143
Reputation: 437111
I assume that this is what you're looking for:
PS> $Object | % { $_.PropertyC[0] }
ValueC_1.1
ValueC_2.1
ValueC_3.1
ValueC_4.1
That is, for each object in $Object
, you want to return its array-valued .PropertyC
's property's 1st element.
Note:
%
is a built-in alias of ForEach-Object
, which accepts a script block ({ ... }
) to execute for each pipeline input object.
Inside that script block, the automatic $_
variable refers to the pipeline input object at hand.
Upvotes: 5