1

I have created a small script to extract all lines of a specified string for a folder and its subfolder. It behaves as expected untill I want to export the lines to excel (csv). At that point it only exports two lines (at least it is the same two lines every single time). In reallity it should have exportet around 40 lines. The working script looks like this:

[string]$pathSource = "D:\Projects\test"
[string]$pathTarget = "D:\Projects\test\searchpattern-sql.csv"

[string[]]$excludeFileTypes = "*.dll","*.pdb","*.gif","*.dat","*.bin","*.msi","*.epi4","*.png","*.tlog","*.gif","*.cache","*.csproj","*.config","*.user","*.txt"

[string]$searchPattern = "sqlCommand"

Get-ChildItem $pathSource -Exclude $excludeFileTypes  -Recurse | Select-String $searchPattern | Sort-Object Path | Select-Object Path, LineNumber, Line

Adding a new pipe with the export cmdlet ( | Export-Csv -path $pathTarget) somehow ruins the search. What am I doing wrong?

3
  • Can you show the two lines by editing your question? Commented May 24, 2011 at 16:37
  • Do you have any sample input/output that demonstrates the problem? Commented May 24, 2011 at 17:24
  • @zdan, the real problem turned out to be Excel, the script itself worked just fine. Commented May 25, 2011 at 5:00

2 Answers 2

1

Just test this :

[string]$pathSource = "D:\Projects\test"
[string]$pathTarget = "D:\Projects\test\searchpattern-sql.csv"

[string[]]$excludeFileTypes = "*.dll","*.pdb","*.gif","*.dat","*.bin","*.msi","*.epi4","*.png","*.tlog","*.gif","*.cache","*.csproj","*.config","*.user","*.txt"

[string]$searchPattern = "sqlCommand"

$a = Get-ChildItem $pathSource -Exclude $excludeFileTypes  -Recurse | Select-String $searchPattern | Sort-Object Path | Select-Object Path, LineNumber, Line
$a | Export-Csv yourfile.csv -NoTypeInformation

The $a var just contains the array of lines that I export.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that made a difference in Excel, but it was still not correct. And that can't be, there must be something else going on. And there was --> Excel. Excel when opening the .csv file was trying to be clever and failed. Only noticed this when opening the file using Notepad++. Tried my original script and opended the result in Notepad++ and the info was all there. So i guess the real answer is "don't always trust Excel when opening .csv files".
0

Personally I've found problems using Export-CSV. What I've found working fine is pipe to ConvrtTo-CSV and then to Set-Content or output to file using redirection.

2 Comments

No, I get the same result doing it that way. The ConvertTo-CSV works just fine but when I pipe it into the Set-Content cmdlet it is back to the same two lines...
the real problem was Excel, the script did what it was supposed to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.