Monthly Archives: September 2010

File Content Search in PowerShell

There is a way to use Powershell to search the contents of a file.  This method will not use the Windows index so it will be slow, but it does work.

get-childitem c:users -filter *.txt -recurse | select-string -list -pattern “yourtext” | foreach {$_.Path}

Explanation

  • get-childitem: Get the children for a container.  In this example the container is a folder on the file system.
    • The first argument is the directory to search (c:users).
    • The –filter switch is used to limit what files to search, in this example it will search files with a txt extension.
    • Process all sub-directories with the –recurse switch.
  • select-string: Find text in strings or files.
    • The –list switch stops the search within the source once the match is found.  In other words it will not waste time looking for other occurrences of the text.
    • The –pattern switch is where you specify the search text.  Regex is permitted.
  • foreach:  Iterate the resulting files.
  • $_.Path:  Output the full path for the file.