Search code examples
powershellpowershell-3.0pester

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


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?


Solution

  • Use:

    $Items.Title -contains 'foo'