There are-contain,-like,-in operators in the PowerShell, and these operators make it easy to find element content in the array. Where the in operator appears to be in PowerShell 3.0.
Take a look at an example, put the file name of all files in the Windows directory into the array $name, and then look for the Exploer.exe element in the array $name. And see the charm of-contains!
Copy Code code as follows:
ps> $names = Get-childitem-path $env: windir | Select-object-expandproperty Name
ps> $names-contains ' explorer.exe '
True
The-contains operator is really powerful, but unfortunately it cannot contain wildcard characters in the specified string. If you want to use wildcards to find array elements, you can use the-like operator.
Copy Code code as follows:
ps> $names-contains ' explorer* '
False
The above example shows that-contains cannot use wildcard characters, let's use-like to see.
Copy Code code as follows:
ps> $names-like ' explorer* '
Explorer.exe
The first small part of the article also says that you can use the-in operator for similar processing, and the in operator can also reverse the array and the string to be matched. What does that mean? Let's look at a few examples.
Copy Code code as follows:
Ps> ' Peter ', ' Mary ', ' Martin '-contains ' Mary '
True
Ps> ' Peter ', ' Mary ', ' Martin '-contains ' ma* '
False
Ps> ' Mary '-in ' Peter ', ' Mary ', ' Martin '
True
Ps> ' Peter ', ' Mary ', ' Martin '-like ' ma* '
Mary
Martin
ps> @ (' Peter ', ' Mary ', ' Martin '-like ' ma* '). COUNT-GT 0
The meaning of these examples is understood by all. About using PowerShell in the array to find elements, small series on the introduction of so many, hope to help everyone.