Search through strings or files for patterns.

      Select-String { -inputObject PSObject | [-path] string[] |  -LiteralPath string[] }
         [-CaseSensitive] [-Context Int32[]] [-Culture String] [-Encoding Encoding]
            [-Pattern] string[] [-Raw] [-SimpleMatch] [-Quiet]
               [-List] [-NoEmphasis] [-Include string[]] [-Exclude string[]]
                  [-NotMatch] [-AllMatches] [CommonParameters]

       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.
       This parameter is ignored when used in combination with the SimpleMatch parameter.
       If you wish to return all matches and the pattern that you are searching for contains
       regular expression characters, you must escape those characters rather than using SimpleMatch.

       Make the matches case sensitive. By default, matches are not case-sensitive.

   -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.

   -Culture String
       Specifies a culture name to match the specified pattern.
       The Culture parameter must be used with the SimpleMatch parameter.
       The default behavior uses the culture of the current PowerShell runspace (session).
       To get a list of all supported cultures, use Get-Culture -ListAvailable command.
       In addition, this parameter accepts the following arguments:
          CurrentCulture  The default;
          Ordinal         Non-linguistic binary comparison;
          Invariant       Culture independent comparison.
       With Select-String -Culture Ordinal -CaseSensitive -SimpleMatch command you get the fastest binary comparison.
       The Culture parameter uses tab completion to scroll through the list of arguments that specify the
       available cultures. To list all available arguments, use the following command:
          (Get-Command Select-String).Parameters.Culture.Attributes.ValidValues

   -Encoding string
The character encoding of the file being searched. ASCII ASCII (7-bit) character set. BigEndianUnicode UTF-16 format big-endian byte order. oem Use the default encoding for MS-DOS and console programs. default (PowerShell 5) encoding corresponding to the active code page (usually ANSI). UTF8 UTF-8 format. utf8BOM UTF-8 format with Byte Order Mark (BOM) utf8NoBOM UTF-8 format without Byte Order Mark (BOM) utf32 UTF-32 format. UTF7 UTF-7 format. Unicode UTF-16 format little-endian byte order. The default is UTF8NoBOM. Beginning with PowerShell 6.2, the Encoding parameter also allows numeric IDs of registered code pages (like -Encoding 1251) or string names of registered code pages (like -Encoding "windows-1251"). For more information, see the .NET documentation for Encoding.CodePage. -Exclude string Omit the specified items from the Path e.g. "*SS64*" The value of this parameter qualifies the Path parameter. Wildcards are permitted. -include string Include only the specified items from the Path. e.g. "May*" The value of this parameter qualifies the Path parameter. Wildcards are permitted. -inputObject psobject Accept an object as input to Select-String. Enter a variable, command or expression that gets the objects. Using -InputObject isn’t the same as sending strings down the pipeline to Select-String. When you pipe more than one string to the Select-String cmdlet, it searches for the specified text in each string and returns each string that contains the search text. When you use -InputObject to submit a collection of strings, Select-String treats the collection as a single combined string. Select-String returns the strings as a unit if it finds the search text in any string. -List Return only the first match in each input file. By default, Select-String returns a MatchInfo object for each match found. -Path path Strings or files to match against, wildcards are allowed. -LiteralPath Specifies the path to the files to be searched. The value of the LiteralPath parameter is used exactly as it’s typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences. -NoEmphasis By default, Select-String highlights the string that matches the pattern you searched for with the Pattern parameter. The NoEmphasis parameter disables the highlighting. The emphasis uses negative colors based on your PowerShell background and text colors. For example, if your PowerShell colors are a black background with white text. The emphasis is a white background with black text. This parameter was introduced in PowerShell 7. -NotMatch Find text that does not match the specified pattern. -Pattern string The string or regular expression to match. If a string is entered, use the -SimpleMatch parameter. -Quiet Suppress most of the output from Select-String. When specified, only a boolean value (True or False) is passed along the pipeline, instead of a MatchInfo object. TRUE = a match was found, otherwise FALSE. This parameter cannot be specified along with -raw -Raw Causes the cmdlet to output only the matching strings, rather than MatchInfo objects. This is the results in behavior that’s the most similar to the Unix grep or Windows findstr.exe commands. This parameter was introduced in PowerShell 7. -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. Also, when SimpleMatch is used, the Matches property of the MatchInfo object returned is empty. Note When this parameter is used with the -AllMatches parameter, the AllMatches is ignored.

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.

You can pipe any object that has a ToString method to Select-String.

Standard Aliases for Select-String: sls


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.
Windows CMD: FC - Compare two files and display differences.
Equivalent bash command: grep - Search files for specific text.

Copyright © 1999-2024
Some rights reserved