find all files and lines that contains a specific string

0

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.

Nora Seinfield

Posted 2018-11-05T15:22:44.747

Reputation: 3

Where do you want to add which lines? – harrymc – 2018-11-05T15:51:25.397

A simple gci 'C:\Users\xxx\*.rdl' -r|sls -Pattern '/Documents/' should already do what you want. – LotPings – 2018-11-05T16:16:52.673

I wish to add the line for every file that contains the string "documents". But I can't get it to work. The output needs to be in two columns if possible. One with the path of the file and the other with the line that contains the string. – Nora Seinfield – 2018-11-05T19:58:46.443

Answers

3

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  

postanote

Posted 2018-11-05T15:22:44.747

Reputation: 1 783