Shell – find all files and lines that contains a specific string

file searchpowershell

I'm quite new with powershell, and I need to help my colleague finding all files in a folder that contains the word /Documents/.

The output has to be in a text file containing both the path and the line in that file.

As a start I've managed to extract the paths using the following code. But I can't manage to include the following lines:

$path = 'C:\Users\XXX'
$Text =”/Documents/"
$PathArray = @()

Get-ChildItem $path -Filter *.rdl -Recurse |
 ForEach-Object { 
 If (Get-Content $_.FullName | Select-String -Pattern $Text ){
            $PathArray += $_.FullName
            $PathArray += $_.FullName

           #write-Host "jhk"
         }
    $PathArray | % {$_} | Out-File "C:\Users\XX\tes2.txt"-Append
 }
 Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}

This code works, but as said, I'm not sure how to add the lines as well.

Best Answer

It's critical that if you are new, that you first spend the time to ramp up to prevent a lot of unnecessary frustration and confusion you are going to encounter.

Do a search on Microsoft Virtual Academy on PowerShell and YouTube for no cost video training.

Here are some other resources and advice:

  • Free eBooks are available on this site
  • Read the full help file on any cmdlet you are trying to use
  • Practice with the examples
  • Read the helpfile again
  • Pick up a few good books, like, 'PowerShell in a Month of Lunches'.
  • There are lots of free PowerShell eBooks on Microsoft's websites and others.

See also: The PowerShell Survival Guide

As for a specific example on your question. How about this approach?

$searchWords = @('Hello','Client')

Foreach ($sw in $searchWords)
{
    Get-Childitem -Path "d:\temp" -Recurse -include "*.txt","*.csv" | 
    Select-String -Pattern "$sw" | 
    Select Path,LineNumber,@{n='SearchWord';e={$sw}}
}


# Partial Results

Path                                            LineNumber SearchWord
----                                            ---------- ----------
D:\temp\Duplicates\BeforeRename1\PsGet.txt             157 Hello     
D:\temp\Duplicates\BeforeRename1\PsGet.txt             161 Hello     
D:\temp\Duplicates\BeforeRename1\StringText.txt          1 Hello     
D:\temp\Duplicates\PoSH\PsGet.txt                      157 Hello     
D:\temp\Duplicates\PoSH\PsGet.txt                      161 Hello     
D:\temp\Duplicates\PoSH\StringText.txt                   1 Hello     
...    
D:\temp\Duplicates\BeforeRename1\PoSH-Get-Mo...        108 Client    
D:\temp\Duplicates\BeforeRename1\Powershell ...         12 Client    
D:\temp\Duplicates\BeforeRename1\Powershell ...         15 Client    
... 
D:\temp\Duplicates\BeforeRename1\WindowsFeat...         92 Client    
D:\temp\Duplicates\BeforeRename1\WindowsFeat...         94 Client    
D:\temp\Duplicates\BeforeRename1\WindowsFeat...        149 Client    
D:\temp\Duplicates\BeforeRename1\WindowsFeat...        157 Client    
D:\temp\Duplicates\BeforeRename1\WindowsFeat...        191 Client    
D:\temp\Duplicates\BeforeRename1\WindowsFeat...        239 Client    
D:\temp\Duplicates\BeforeRename1\WindowsFeat...        241 Client  
Related Question