3

I want to search a folder the the recurse subfolder's files for a certain string (e.g. "Banana"). I can't seem to get it to work...

This is what I got:

$Searchstring='Banana'
$PathArray=@()
$Path='C:\users\myuser'
$content= Get-Content $_.FullName 

 Get-ChildItem $Path | 
  Where-Object { $_.Attributes -ne "Directory"}

      ForEach-Object ( 
         If ($content | Select-String -Pattern $Searchstring) {
           $PathArray += $_.FullName

         }
      )

unfortunately, this script does not do what I want.

2 Answers 2

2

Here is a very simple solution:

$SearchInDirectory = "C:\SearchingFolder";
$SearchForString = "Banana"; #String for searching
$SearchInFormats = "*.asp", "*.txt", "*.js"; #If empty will search in all file formats

echo 'Searching in progress...'
Get-ChildItem  $SearchInDirectory  -include $SearchInFormats   -recurse  |  Select-String  -pattern  $SearchForString  -SimpleMatch  |  group path  |  select name

It searches (recursively) in all directories and sub-directories for all the results and then prints them, so if you have a lot of files and directories it may take some time. Also, you can specify the file types you need to check (but not necessarily).

And also, I have a script that puts the results into file:

$SearchInDirectory = "C:\SearchingFolder";
$SearchForString = "Banana"; #String for searching
$SearchInFormats = "*.asp", "*.txt", "*.js"; #You can specify the file types
$PutSearchResultsInFile = ""; #link to a file or if blank - the results to be displayed on screen

Cls;

function SearchFilesByString_results () {
    Get-ChildItem  $SearchInDirectory  -include $SearchInFormats   -recurse  |  Select-String  -pattern  $SearchForString  -SimpleMatch  |  group path  |  select name
}

if($PutSearchResultsInFile){
        if(!(Test-Path -Path $PutSearchResultsInFile)){
           New-Item $PutSearchResultsInFile -itemType "file" -confirm:$false | Out-Null 
           echo "`nNew file created. `n";
        } 
        echo "Searching in progress... `n";
        SearchFilesByString_results > $PutSearchResultsInFile;
        echo "Search completed! Find results here: " $PutSearchResultsInFile "`n";
} else {
    echo "`nSearching in progress.... `n";
    SearchFilesByString_results
    echo "`nSearch completed! `n";
}

Hope it helps! :)

Sign up to request clarification or add additional context in comments.

Comments

0

You are not updateing the $content which you are using to search. Also, you can simplify this:

$Searchstring='Banana'
$Path='C:\users\myuser'

$PathArray = Get-ChildItem $Path -File | Select-String -Pattern $Searchstring | select -ExpandProperty Path

3 Comments

You don't need the ForEach loop, You can piple the Get-Childitem directly into Select-String like so: Get-ChildItem $Path -File | Select-String -Pattern $Searchstring | select -ExpandProperty FileName
@DaveSexton Thanks for the hint.
This shows only the file names, while the guy wants to see the path too. You can add that too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.