find all files and lines that contains a specific string
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