PowerShell using the LIKE operator with wildcards to find string examples _powershell

Source: Internet
Author: User

This article describes using wildcards to find strings in PowerShell, and uses the LIKE operator with wildcard characters.

The wildcard characters in PowerShell are the asterisk (*) and question mark (?), which is the same as in the DOS system. To find in a string using wildcard characters, you can use the LIKE operator. Examples are as follows:

"123" is started with 1

Copy Code code as follows:
PS c:\users\splaybow> "123"-like "1*"
True

"123" is with 1 beginning, 3 ending
Copy Code code as follows:
PS c:\users\splaybow> "123"-like "1*3"
True

"123" contains 2
Copy Code code as follows:
PS c:\users\splaybow> "123"-like "*2*"
True

"123" contains 1
Copy Code code as follows:
PS c:\users\splaybow> "123"-like "*1*"
True

"123" has 3 characters
Copy Code code as follows:
PS c:\users\splaybow> "123"-like "???"
True

"123" has 2 characters
Copy Code code as follows:
PS c:\users\splaybow> "123"-like "??"
False

Through some of the above examples, we should have a more profound understanding of the-like operation Fugaton wildcard to achieve the string lookup.

It should be explained that the return value of the LIKE operator is a Boolean value, that is, true or flase. It can be used in conditional judgments, such as if statements, or in where-object statements. The following example lists the programs that are currently running, and the program name begins with the letter "C".

Copy Code code as follows:

PS c:\users\spaybow> get-process |? {$_.name-like "c*"}

Handles NPM (k) PM (k) WS (k) VM (M) CPU (s) Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
5 2264 3424 0.02 5332 cmd
6 1860 5208 1064 Conhost
8 2916 7736 Bayi 0.27 5304 conhost
637 2264 5148 Csrss
540 2904 29252 216 624 Csrss


Note: The question mark (?) in the PowerShell statement above is an alias for Where-object.

About PowerShell use like plus wildcard character to find a string, this article on the introduction of so many, I hope to help you, thank you!

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.