geek_01
geek_01

Reputation: 585

In Powershell, How do I get the Values out of an array returned by an object?

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

Answers (1)

mklement0
mklement0

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:

Upvotes: 5

Related Questions