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!