Ashwin Nanjappa
Ashwin Nanjappa

Reputation: 78558

PowerShell: Why does Out-File break long line into smaller lines?

Try this little experiment. Create a file Foo.txt with a very long line of text (say 500 chars long) like this:

// Foo.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

Now issue the following command:

$ Get-Content Foo.txt | Select-String "a" | Out-File Foo2.txt

You will find that the long line of string has been broken down into smaller lines in Foo2.txt. The length of each smaller line is the same as the console width.

Why does Out-File break the long line into smaller line when the output is not headed to the console?

And why does Out-File not break down the lines for the following command?

$ Get-Content Foo.txt | Out-File Foo3.txt

Upvotes: 24

Views: 20244

Answers (3)

Tahir Hassan
Tahir Hassan

Reputation: 5817

$ Get-Content Foo.txt | Select-String "a" | Add-Content Foo2.txt

Use Add-Content (or you could use Set-Content if you wish to overwrite the file).

Upvotes: 14

Polymorphix
Polymorphix

Reputation: 1075

You can adjust where Out-File breaks lines using the -width parameter

$ Get-Content Foo.txt | Select-String "a" | Out-File -width 1000 Foo2.txt

Upvotes: 26

JPBlanc
JPBlanc

Reputation: 72680

This can be explained by the fact that the result of Get-Content Foo.txt | Select-String "a" is of type MatchInfo, it's not a string.

just test :

Get-Content Foo.txt | Select-String "a" | Format-list *

Upvotes: 9

Related Questions