Problem
You want to remove all elements from an array that match a given item or term— either exactly, by pattern, or by regular expression.
Solution
To remove all elements from an array that match a pattern, use the –ne, notlike, and –notmatch comparison operators as shown in Example 112.
Example 112. Removing elements from an array using the –ne, notlike, and –notmatch operators
PS >$array = "Item 1","Item 2","Item 3","Item 1","Item 12" PS >$array ne "Item 1" Item 2 Item 3 Item 12 PS >$array notlike "*1*" Item 2 Item 3 PS >$array notmatch "Item .." Item 1 Item 2 Item 3 Item 1
To actually remove the items from the array, store the results back in the array:
PS >$array = "Item 1","Item 2","Item 3","Item 1","Item 12" PS >$array = $array ne "Item 1" PS >$array Item 2 Item 3 Item 12
Discussion
The eq, like, and match operators are useful ways to find elements in a collection that match your given term. Their opposites—the –ne, notlike, and –notmatch operators—return all elements that do not match that given term.
To remove all elements from an array that match a given pattern, then, you can save all elements that do not match that pattern.