Search a folder/files for string of text

Search a folder/files for string of text 2.1


I was looking for a Windows application that would allow me to search some files in the same folder for a particular string of text, but many of the apps to do this were fairly expensive. So, I made use of a few lines of PowerShell code to do this for free!

This script will prompt you for the folder to search (it will search ALL files in this folder and any sub-directories), then it will ask you for the string of text to search for.

The filename and line of text are then listed, so you can locate your search result.

Here is the script:

Code:
Function Get-SearchFolder {
  $object = New-Object -comObject Shell.Application
  $folder = $object.BrowseForFolder(0, "Which folder do you wish to search (includes subfolders)", 0)
  if ($folder -ne $null) {
    $searchfolder = $folder.self.Path.substring(0,$folder.self.path.length)
    if ($folder.self.path.substring($folder.self.path.length - 1, 1) -ne "\") {
      $searchfolder = $folder.self.Path.substring(0,$folder.self.path.length) + "\"
    }
  }
  Set-Location $searchfolder
}

Write-Host 'Which folder do you wish to search (includes subfolders)'
$searchfolder = Get-SearchFolder
$textstring = Read-Host -Prompt 'Please enter the string to search for'
Get-ChildItem $searchfolder -Filter *.* -Recurse | Select-String $textstring
Read-Host -Prompt 'Search Complete. Press ENTER to exit...'
I have attached the complete PowerShell script as a download to this article. Just download it to your PC, then when you want to run it, right click it and select "Run with PowerShell". Double-clicking the file will likely open it in an editor, rather than running the script.

If you have any comments or wish to modify this, let me know in the discussion thread and I will do my best to help.

Author
Ian
Downloads
2,101
First release
Last update

More resources from Ian

Latest updates

  1. Fixed Username Tag

    Fixed empty user name tag
  2. Added file browser box

    Updated the script to include a file browser, using @Regedit32's modification.
Top