Skip to main content

Find Items in an Array That Match a Value

Problem

You have an array and want to find all elements that match a given item or term— either exactly, by pattern, or by regular expression.

Solution

To find all elements that match an item, use the –eq, like, and –match comparison operators:

PS >$array = "Item 1","Item 2","Item 3","Item 1","Item 12" PS >$array eq "Item 1" Item 1 Item 1 PS >$array like "*1*" Item 1 Item 1 Item 12 PS >$array match "Item .." Item 12

Discussion

The eq, like, and match operators are useful ways to find elements in a collection that match your given term. The –eq operator returns all elements that are equal to your term, the –like operator returns all elements that match the wildcard given in your pattern, and the –match operator returns all elements that match the regular expression given in your pattern.

If you need dedicated infrastructure for finding array items that match specific values with PowerShell, consider a Netherlands Dedicated Server for greater control over data filtering, scripting, automation, system administration, and Windows-based application workloads.

Help Category