Determine if Array Contains Value – Using Wildcards

by | March 28,2017

Table of Contents

Checking for an Element in an Array Using PowerShell

If you’d like to know whether an array contains a given element, PowerShell provides the –contains operator. This operator does not support wildcards, though, so you only can check for exact matches.

Using a Workaround to Filter Array Elements with Wildcards

Here is a workaround that helps you filter array elements with wildcards:

$a = 'Hanover', 'Hamburg', 'Vienna', 'Zurich'

# is the exact phrase present in array?
$a -contains 'Hannover'
# is ANY phrase present in array that matches the wildcard expression?
(@($a) -like 'Ha*').Count -gt 0

# list all phrases from array that match the wildcard expressions
@($a) -like 'Ha*'

Twitter This Tip! ReTweet this Tip!