Search through strings or files for patterns.

      Select-String {-inputObject psobject | [-path] string[]}
         [-pattern] string[] [-Context Int32[]] [-Encoding string]
            [-include string[]] [-exclude string[]] [-caseSensitive]
               [-List] [-AllMatches] [-NotMatch] [-SimpleMatch]
                  [-Quiet] [CommonParameters]

   -Pattern string
       The string or regular expression to match.
       If a string is entered, use the -SimpleMatch parameter.
   -inputObject psobject
       Accept an object as input to Select-String. Enter a variable,
       command or expression that gets the objects.

   -Path path
       Strings or files to match against, wildcards are allowed.

       Search for more than one match in each line of text.
       Without this parameter, Select-String will find only the first match in each line. 

       When more than one match is found, Select-string still emits only
       one MatchInfo object for the line, but the Matches property of the
       object contains all of the matches.

   -Context Int32[]
       Capture the specified number of lines before and after the line with the match.
       This allows viewing the matching text in context.

       If one number is given as the value of -context, that number of lines will be
       captured before and after the match.

       If two numbers are given as the value of -context, the first number determines the
       number of lines before the match and the second number determines the number of lines after the match.

       In the default display, lines with a match are indicated by a right angle bracket (>)
       in the first column of the display. Unmarked lines are the context.

       This parameter does not change the number of objects generated by Select-String.
       Select-String generates one MatchInfo object for each match.
       The context is stored as an array of strings in the Context property of the object.

       If the output of a Select-String command is piped to another Select-String command,
       the receiving command searches only the text in the matched line
       (the value of the Line property of the MatchInfo object), not the text in the context lines.
       When the context includes a match, the MatchInfo object for each match includes
       all of the context lines, but the overlapping lines appear only once in the display.

   -Encoding string
The character encoding of the file being searched. The default is UTF8.

Valid values are "UTF7", "UTF8", "UTF32", "ASCII", "Unicode", "BigEndianUnicode", "Default", and "OEM". "Default" is the encoding of the system's current ANSI code page. "OEM" is the current original equipment manufacturer code page for the OS. -include string Include only the specified items from the Path. e.g. "May*" this only works when the path includes a wildcard character. -Exclude string Omit the specified items from the Path e.g. "*SS64*" this only works when the path includes a wildcard character. -CaseSensitive Make the matches case sensitive. By default, matches are not case-sensitive. -Quiet Suppress most of the output from Select-String. When specified, only a boolean value is passed along the pipeline. TRUE = a match was found, otherwise FALSE. -List Return only the first match in each input file. By default, Select-String returns a MatchInfo object for each match found. -NotMatch Find text that does not match the specified pattern. -SimpleMatch Use a simple match rather than a regular expression match. This will search the input for the text given in the -Pattern parameter. It will not interpret -Pattern as a regular expression.

The output of Select-String command is not a collection of strings, but a collection of MatchInfo objects. The Line property of the MatchInfo object contains a string with the matching results.


Perform a case sensitive matching of literal strings:

PS C:\> "Hello","HELLO" | select-string -pattern "HELLO" -casesensitive

Search through all files with the .xml file extension in the current directory and display the lines in those files that include the case sensitive string "Princess":

PS C:\> select-string -path *.xml -pattern "Princess" -casesensitive

Retrieve the last 100 events from the application event log and filter to show only those containing the message string "failed":

PS C:\> $events = get-eventlog -logname application -newest 100
PS C:\> $events | select-string -inputobject {$_.message} -pattern "failed"

Now include 2 lines before and 3 lines after each matching line - line numbers will appear in the output with a > prefixing the matching line:

PS C:\> $events | select-string -inputobject {$_.message} -pattern "failed" -Context 2,3

Examine all files in the subdirectories of C:\Windows\System32 with the .txt file name extension and search for the Case-Sensitive string "Microsoft":

PS C:\> get-childitem c:\windows\system32\* -include *.txt -recurse |
select-string -pattern "Microsoft" -casesensitive

Select lines from a file containing "Total Sales" and then split the matching lines:

PS C:\> $demo = get-content C:\demo\sales.txt
PS C:\> $demo | select-string "Total Sales" | %{$_.line.split()}

"The key insight is that the smallest constituents of the world are not particles, as had been supposed since ancient times, but "strings" - tiny strands of energy" - Jim Holt, (The New Yorker)

Related PowerShell Cmdlets:

Get-Help - about_commonparameters.
Get-Help - about_comparison_operators.
Get-Help - about_regular_expression.
Equivalent bash command: grep - Search files for specific text.

Copyright © 1999-2021
Some rights reserved