Avoid line breaks when using out-file Avoid line breaks when using out-file powershell powershell

Avoid line breaks when using out-file


Keep in mind that Select-String outputs MatchInfo objects and not strings - as is shown by this command:

gci $logdir -r *.txt | gc | select-string $patterns | format-list *

You are asking for an implicit rendering of the MatchInfo object to string before being output to file. For some reason I don't understand, this is causing additional blank lines to be output. You can fix this by specifying that you only want the Line property output to the file e.g.:

gci $logdir -r *.txt | gc | select-string $patterns | %{$_.Line} |     Out-File $csvpath -append -width 1000


Why don't you use Add-Content?

gci $logdir -rec *.txt | gc | select-string $pattern | add-content $csvpath

You don't need to specify the width and -append switch, the file size is not doubled by default (although you can specify encoding) and it seems that there is no problem with the empty lines like you have.