Hi snofire, try this:
Select-String -pattern "$term" -context 20,20 -caseSensitive -Path <path>\*.*
-list | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}
Since you copy each matched file full content to another file, I don't think
the -context 20,20 is necessary.
If you need to perform recursive search, use:
get-childitem <path> -filter <wildcard> -recurse | Select-String -pattern
"$term" ... | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}
-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog:
http://blogs.microsoft.co.il/blogs/scriptfanatic Quote:
> Hey all,
>
> I need a powershell script that will search through all of the text
> files in a directory, and copy those files that contain a given term
> into another file. This is the line I am having trouble with:
>
> Get-Content *.* | Select-String -pattern "$term" -context 20,20
> -caseSensitive | Out-File E:\test.txt
>
> With this, the script locates the terms all right, but because I had
> to use context 20,20 in order to get the whole file where the term
> lives, it grabs a bunch of useless data as well. Is there any way that
> I can just paste a file where the term occurs?
>