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

Erwin Pombett
Erwin Pombett used Ask the Experts™

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
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
If you need that access often, you should better create a hashtable with the key (here: lastname) as index.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial