This post aims to explore the primary and advanced use of the PowerShell Select-String cmdlet.
How to use PowerShell select-string cmdlet
In PowerShell, the Select-String cmdlet can be used to search for a single pattern or multiple patterns in the file or string. The following syntax is used to refer Select-String cmdlet:
The following parameters can be used with the Select-String cmdlet:
- Pattern: Identifies the text to be selected in the file.
- Path: Refers to the path of the file.
- CaseSensitive: Used to match the terms by matching the letter case.
- NotMatch: Utilized to get the result that does not contain the specified text\pattern.
- AllMatches: specifies finding more than one match in each line of the file.
Example 1: To select the specific pattern (text) in a file
The sample code is as follows to select the pattern (i.e., text/string) in the “Sample100.csv” file. Firstly, the absolute path of the “Sample100.csv” is given after the “-Path” parameter. Afterwards, the pattern “Mark” is set using the parameter “-Pattern”:
The pattern was “Mark” and it is observed that the Select-String has printed the line (along with the line number) that contains the “Mark” in it.
Example 2: To select the multiple patterns (text) in a file
You can select or find multiple patterns within the file. To do so, the text to be searched is provided after the “-Pattern” parameter. The following command searches for the “Mark” and “Peter” keywords inside the “Sample.csv” file:
As you can see, those lines are printed that contain either Mark or Peter.
Example 3: To select only limited records of specified patterns (text) in a file
The Select-String cmdlet can be used to search for the specific pattern in a file, but only the limited number of instances would be searched. In the given-below example, the “.csv” file is being filtered for the pattern “Mark”. However, the limit is set to 2 using the “-First” parameter:
In the above output, the first two occurrences of the “Mark” keyword are displayed on the PowerShell terminal.
Example 4: To select the patterns in specified columns
The Select-String cmdlet can be utilized with the Select-Object to select match the pattern and return specific properties of the line containing those patterns. For this, the pattern matching is specified in the Select-String cmdlet, and the filters will be applied in the Select-Object.
In our case, the following example command exercises the “Select-String” cmdlet to match patterns “Peter” or “Phil” in a CSV file. This Select-String command is then piped with the Select-Object cmdlet to get the LineNumber, Pattern, FileName, and Line that contains the patterns Peter or Phil:
It is observed from the output that the LineNumber, Pattern, Filename, and Lines are printed that contain the patterns, Peter or Phil.
Example 5: To select the “case sensitive” patterns (text) in a file
You can match a pattern with “-CaseSensitive” parameter to consider the letter case as well. The example command searches for the lines that contain “PETER”:
As you can see, only three records are printed that contain the PETER keyword.
Example 6: To select the unmatched patterns (text) in a file
With the help of the “-NotMatch” parameter, we can select only those records that do not match the specific keywords. In the following example, we have specified two patterns (“Mark” and “Peter”) in the -NotMatch parameter:
As it can be seen in the above records, the record printed on the console neither contains “Mark” nor “Peter”.
Conclusion
In PowerShell, the PowerShell Select-String cmdlet is utilized to find the specified text/pattern in the file. The use of the Select-String command can be enhanced by utilizing different parameters (-NotMatch, -CaseSensitive) and properties (FileName, Line, Pattern). Moreover, you can extend the usage of the Select-String cmdlet by using it with Select-Object. You have learned the working of the Select-String cmdlet and its use cases in various scenarios.