Powershell: Searching in arrays

Hi,

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

This is our array

$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

$aMySearchArray -contains "entry1"
True

The same case sensitive

$aMySearchArray -ccontains "entry1"
False

Check if an entry is not present, case insensitive

$aMySearchArray -notcontains "Entry10"
True

and case sensitive

$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”

$aMySearchArray -like "entry*"
Entry
Entry1
entry2
entry2
Entry3
Entry4

Same case sensitive

$aMySearchArray -clike "entry*"
entry2
entry2

And the same negotiated case insensitive

$aMySearchArray -notlike "Entry?"
Entry

And case sensitive

$aMySearchArray -cnotlike "Entry?"
Entry
entry2
entry2

Michael

Leave a Reply Cancel reply