Powershell: Searching in arrays

Hi,

this is a really often requiered task in powershell. Here are some examples.

This is our array

1
2
3
4
5
6
7
8
$aMySearchArray=@(
    "Entry",
    "Entry1",
    "entry2",
    "entry2",
    "Entry3",
    "Entry4"
)

Use the contain operator to search for an whole entry case insensitive. The contain operators simply returns $true or $false

1
2
$aMySearchArray -contains "entry1"
True

The same case sensitive

1
2
$aMySearchArray -ccontains "entry1"
False

Check if an entry is not present, case insensitive

1
2
$aMySearchArray -notcontains "Entry10"
True

and case sensitive

1
2
$aMySearchArray -cnotcontains "Entry1"
False

By using the like operator it is also possible to use wildcards for searching. * is a wildcard for non,one or more than one character, ? exactly for one character
This searches case insensitve for all items which begins with “entry”

1
2
3
4
5
6
7
$aMySearchArray -like "entry*"
Entry
Entry1
entry2
entry2
Entry3
Entry4

Same case sensitive

1
2
3
$aMySearchArray -clike "entry*"
entry2
entry2

And the same negotiated case insensitive

1
2
$aMySearchArray -notlike "Entry?"
Entry

And case sensitive

1
2
3
4
$aMySearchArray -cnotlike "Entry?"
Entry
entry2
entry2

Michael

Leave a Reply