How to “grep” for multiple strings in files on Windows with PowerShell

So you want to search for multiple strings in your files on Windows?

Sure, you can do the job several ways, but if you have PowerShell installed on your box, you might use this snippet :

get-childitem "folder_where_to_look" -include *.txt -rec | where { $_ | Select-String -Pattern ‘find_me_1’ } | where { $_ | Select-String -Pattern ‘find_me_2’ }

Basically what this snippet does this :

  • find all .txt files in supplied folder and recursively in all subfolders, pass this list to
  • first pattern search for “find_me_1 string, than pass list of matching files with this string to
  • second pattern search
  • pass result to console

Nice this is, that you can also use RegEx in patterns that will be searched for and you can also dump found files to file for later use.

Hope this helps someone.

One comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Todd Harvey · February 25

    thanks so much

    I think the below works:

    find all files that match any in a set of strings

    $path = “d:\tfs”
    $includes = @(“.cs” , “.vb” , “*.sql” )
    $pattern = @( “ODSMGR” , “UTK_” )
    get-childitem $path -include $includes -rec | where { $_ | Select-String -Pattern $pattern }