Check Array Content With Wildcards

by Jun 20, 2011

You may know the -contains operator. Try using it to check whether an array contains a specific element:

PS > $names = dir $env:windir | Select-Object -ExpandProperty Name
PS > $names -contains 'explorer.exe'
True

 

However, -contains does not support wildcards:

PS > $names -contains 'explorer*'
False

 

You can simply use -like instead. It works on arrays, too:

PS > $names -like 'explorer*'
explorer.exe
PS > @($names -like 'explorer*').Count -gt 0
True

 

 

Twitter This Tip!
ReTweet this Tip!