-8

I have found an answer that gives the list of files in powershell here

How to search a string in multiple files and return the names of files in Powershell?

but I need to know the line number and line text also where that text is found. After this I need to write the output to an excel or csv.

edit: Get-ChildItem -recurse | Select-String -pattern "ftp" | Write-Host script is giving me the output as C:\codes\prog1.txt:10:FTP but I want the output as path - C:\codes\prog1.txt line number-10 Line text-FTP "server1" in Excel

Community
  • 1
  • 1
Rajat Gupta
  • 72
  • 1
  • 13
  • 2
    Please provide a minimal working sample code describing your problems and efforts! – ρss Sep 30 '15 at 08:10
  • Get-ChildItem -recurse | Select-String -pattern "ftp" | Write-Host script is giving me the output as C:\codes\prog1.txt:10:FTP but I want the output as path - C:\codes\prog1.txt line number-10 Line text-FTP "server1" in Excel – Rajat Gupta Sep 30 '15 at 08:28
  • Please edit the question and post the code there instead of in a comment! – ρss Sep 30 '15 at 08:37

1 Answers1

2

Try this (don't know if you only want the filename or the path to the file, just remove the one you dont want):

Get-ChildItem -recurse | Select-String -pattern "string" | Select-Object path,line,linenumber,filename | Export-Csv -Path c:\somepath\result.csv
Oggew
  • 366
  • 1
  • 9
  • Thanks Oggew, yes it worked as expected. Thanks a lot for quick response.!! – Rajat Gupta Sep 30 '15 at 10:17
  • Hi I am using the below script for as many strings I want but there is problem with the below script that is creating separate excel for each string in the input file. I tried creating just one file but it is overriding the previous content so came up with different files for different string. Do you know any way on how to append data in the same(one) excel sheet?? $content = Get-Content ..\KeyWord.txt cd "..\Test\" ForEach ($Word in $content){ Get-ChildItem -recurse | Select-String -pattern $Word | Select-Object path,line,linenumber,filename | Export-csv -Path "..\result_$word.csv" } – Rajat Gupta Oct 27 '15 at 06:52
  • What if you add -Append like this: "Export-Csv -Path c:\somepath\file.csv -Append" "Adds the CSV output to the end of the specified file. Without this parameter, Export-CSV replaces the file contents without warning." – Oggew Oct 30 '15 at 15:13
  • The only problem is the system where I need to run the script, its having Powershell 2.0 so if you know any alternative of append? – Rajat Gupta Nov 02 '15 at 07:34
  • This should work: http://stackoverflow.com/questions/21048650/how-can-i-append-files-using-export-csv-for-powershell-2 – Oggew Nov 02 '15 at 08:35
  • ($content = Get-Content ..\KeyWord.txt cd "..\Test\" ForEach ($Word in $content){ Get-ChildItem -recurse | Select-String -pattern $Word | Select-Object path,line,linenumber,filename | Export-csv -Path "..\result_$word.csv") This is the current code which generates suppose 4-5 csv files depending upon the keywords I am giving. There is a way to do it thru append but with the link you just shared http://stackoverflow.com/questions/21048650/how-can-i-append-files-using-export-csv-for-powershell-2 does not do what I need. – Rajat Gupta Nov 02 '15 at 08:44