Determine if an array of PSCustomObject's contains an instance with a property value

33.7k views Asked by At

I need to determine if an array of PSCustomObjects contains an item with its Title property matching a value. I need a Boolean value for use with Pester assertions:

$Items -<function> $Name | Should Be $True

Assuming:

$Items = @()
$Items += [PsCustomObject]@{Title='foo';Url='http://f.io'}
$Items += [PsCustomObject]@{Title='bar';Url='http://b.io'}

Contains does not work:

PS> $Items -contains 'foo'
False

Match returns the matching instance, but it is not a Boolean:

PS> $Items -match 'foo'

Title  Url
-----  ---
foo    http://f.io

I suppose I could:

($Items -Match $Name).Count | Should Be 1

Is there a better option?

1

There are 1 answers

2
Eris On BEST ANSWER

Use:

$Items.Title -contains 'foo'