(21) Wildcard characters in PowerShell

Source: Internet
Author: User

    1. Using wildcard characters for Fuzzy lookup of a file


wildcard characters in Powershell enable fuzzy lookups , which means that a set of items, rather than individual items, can be looked up. For example, if you want to find all files in the D:\MyPowerShell directory that have the. ps1 extension, you can execute the following command:

ps c:\> get-childitem d:\mypowershell\*.ps1     Directory:  D:\ mypowershellmode                 lastwritetime     length name----                 -------------     ------  -----A---         2016-5-26      21:52       1100 3testparamstart.ps1-a---          2016-5-26     22:22        2864 4testparamstart.ps1-a---         2016-5-27      20:55       4536 commentbasedhelp.ps1-a---          2016-5-23     22:20        966  Diskinfo.ps1-a---         2016-5-27      21:26        678 erroraction.ps1 ...

In the above statement, the asterisk (*) is used as a wildcard character to specify any characters that can exist before the. ps1 file name extension. Because the command contains a file name extension, all files returned by the command must have the extension, but not an unlimited file name.


In addition to the asterisk wildcard,several other wildcard characters are supported in PowerShell.

Wildcard characters Description Example
The does not match
* Match with any character A * A, AG, age Baba, Cat
? Matches exactly one character in the specified position ? m Am, IM Ccm
[] Match a group of characters [A-d]ge Age, BGE Fge
[] Matches a specified character [Bc]ook Book, Cook Aook

    powershell accept wildcard characters. the Help for each cmdlet explains which parameters allow wildcards and which can be used with wildcard characters. For arguments that accept wildcard characters, their usage is case-insensitive. For example,? n returns an, a, in, in, on, and on. For example:

PS c:\> get-help get-process-parameter name-name <string[]> Specify one or more processes by process name. You can type more than one process name (separated by commas) or use a wildcard character.    The parameter name ("name") is optional.                    Is it necessary?                        False position?            1 does the default value accept pipeline input?              True (bypropertyname) do you accept wildcard characters? True

From the statement parameter description above, it is known that the-name parameter of Get-process can accept wildcard characters, so the process name can be fuzzy searched. For example:

PS c:\> get-process *shellhandles NPM (k) PM (k) WS (k) VM (M) CPU (s) Id ProcessName------------------ -----------------------------436 6 46828 37320 155 3.20 4116 PowerShell


wildcard characters can be mixed in a single parameter. For example, to find a. ps1 file that starts with the letter m to Q in the D:\MyPowerShell directory, execute the following command:

ps c:\> get-childitem d:\mypowershell\[m-q]*.ps1     Directory:  D:\ mypowershellmode                 lastwritetime     length name----                 -------------     ------  -----A---         2016-5-26      22:58       3172 mandatory.ps1-a---          2016-5-31     22:13         780 manifest.ps1-a---         2016-5-30      22:26       4100 mytools.ps1-a---          2016-5-26     23:18       3408  Paramcount.ps1-a---         2016-5-26      23:10       3368 paramhelp.ps1-a---          2016-5-26     23:34        3672 parampattern.ps1-a---         2016-5-26      23:13       3468 paramset.ps1-a---          2016-5-27     20:44         404 placinghelp.ps1

The above command uses the range wildcard character ([m-q]) to specify that the file name should start with the letter M to Q, and that all files with a. ps1 extension.

2. Using wildcard characters to search for a keyword

Enables wildcard characters to be used to make fuzzy lookups on a specified keyword . For example, to find all text files (. txt) that contain the specified keyword "ERROR" in the current directory (D:\MyPowerShell), you can execute the following statement:

PS d:\mypowershell> Select-string-pattern ("ERROR") *.txthelp.txt:335:-erroraction,-errorvariable,-Wa Rningaction,-warningvariable,help.txt:513:-erroraction,-errorvariable,-warningaction,-WarningVariab Le

The results of the query show which files contain the specified keywords and which lines appear in the above example, respectively, in the 335 and 513 rows.


Fuzzy search and lookup of strings can be useful when parsing a log file. Select-string can be used to search for keywords in a file without opening them, especially if the exact path is not determined.

Summarize

Wildcard characters in Powershell enable fuzzy lookups of files and specified keywords, which are only part of a regular expression, so you can also use regular expressions for pattern matching and fuzzy lookups.

This article from "Flower Blossom Fall" blog, declined reprint!

(21) Wildcard characters in PowerShell

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.