how can i recover an object - looking up on a property - out of an array of custom objects ?


how can i recover an object like we do in linq out of list of custom objects.

$collaborators = Import-Csv $PSScriptRoot/collaborateur.csv -Delimiter ';' 

$PeopleInObjects = @()

foreach($collabo in $collaborators)
    $pObject = New-Object  PSObject

    $pObject | Add-Member -MemberType NoteProperty "Matricule"       $collabo.Matr
    $pObject | Add-Member -MemberType NoteProperty "Firstname"       $collabo.Prenom
    $pObject | Add-Member -MemberType NoteProperty "LastName"        $collabo.Nom

    $PeopleInObjects += $pObject 

Open in new window

// here how can i recover one object after it's lastname ?

thank you in advance
Erwin PombettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
You can use Linq or Where-Object.

Where-Object (because... well PS).
$PeopleInObjects | Where-Object LastName -eq 'SomeValue'

Open in new window

Linq extension methods:
$PeopleInObjects.Where{ $_.LastName -eq 'SomeValue' }

Open in new window

Where-Object returns a scalar if there is one result, Linq will always return a collection. By mindful of this if testing whether or not a value exists.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If you need that access often, you should better create a hashtable with the key (here: lastname) as index.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.