3

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?

0

1 Answer 1

5

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.