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
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
$ 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).
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 *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With