Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Write Context output in a CSV file or any good alternative to Context (Powershell 2.0)

$
0
0
I need to search 10-15 strings into the entire application codes(more than 10000 programs) so I have inserted the strings that i need to search in a text file "strings.text". I also need to know the previous and next line of the matched string line so I am using "Context 1" in the below script. However, the below script is giving output as only Matched String line.
$content = Get-Content strings.txt
ForEach ($Word in $content){
Get-ChildItem -recurse | Select-String -pattern $Word -Context 1 | Select-Object path,line,linenumber,filename | Export-csv -Path "\result_$word.csv"
}
Output:
Path                        Line               LineNumber    FileName
desktop\prog1.txt    Server(xyz)       3               prog1.txt
desktop\prog2.txt    Server(xyz)       6               prog2.txt
What I really want is 
Path                        Line               LineNumber    FileName
                               Connect               2              prog1.txt
desktop\prog1.txt    Server(xyz)       3               prog1.txt
                               stop                     4              prog1.txt
                               Connect               8              prog2.txt
desktop\prog2.txt    Server(xyz)       9               prog2.txt
                               stop                     10             prog2.txt
Can anyone please help how can I get this output?

Viewing all articles
Browse latest Browse all 6937

Trending Articles