Select-String

Search through strings or files for patterns.

Syntax
      Select-String [-pattern] string[] [-text string]
              {-inputObject psobject | [-path] string[]}
                 [-include string[]] [-exclude string[]] [-simpleMatch] 
                    [-caseSensitive] [-quiet] [-list] [CommonParameters]
Key
   -pattern string
       The string or regular expression to match.

   -text string
       Literal text to match against the value of -Pattern.
		
   -Path path
       Strings or files to match against, wildcards are allowed.

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

   -simpleMatch 
        Use a simple match, rather than a regular expression match

   -caseSensitive 
       Make the matches 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 
       Only return info. about the first match from each input file.
        
   -inputObject psobject
       Accept an object as input to Select-String. Enter a variable,
       command or expression that gets the objects.

   CommonParameters:
      -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.

Examples

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 displays 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
$events | select-string -inputobject {$_.message} -pattern "failed"

"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, 2006-10-02)

Related:

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



Back to the Top

Simon Sheppard
SS64.com