PowerShell ways to find array contents, search arrays, and query arrays _powershell

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.