Question Details

No question body available.

Tags

arrays powershell boolean

Answers (1)

Accepted Answer Available
Accepted Answer
June 22, 2025 Score: 2 Rep: 66,944 Quality: High Completeness: 100%

-contains and -in are the PowerShell idiomatic way of doing this type of linear comparison, just as what you have right now is perfectly fine and most likely the most efficient way of doing it linearly. Additionally, both operators will stop at first occurrence, see Containment operators:

These operators stop comparing as soon as they detect the first match...

Another notable mention is that these operators have their negated counterparts, meaning, these comparisons equate:

  • !($boolArray -contains $false) same as $boolArray -notcontains $false
  • !($false -in $boolArray) same as $false -notin $boolArray

For collections of bigger size, you may opt for a hash-based lookup, the types you may often see being used for this type of comparison are HashSet and IDictionary implementing types, such as Hash tables.