Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: why does "Get-Content <file> | Out-File -Append <file>" go into a loop?

Tags:

powershell

I understand why the following will truncate the contents of the file:

Get-Content <file> | Out-File <file>

This is because Out-File is run first and it empties the file before Get-Content has a chance to read it.

But when I try a variant of the above, it goes into a loop:

Get-Content <file> | Out-File -Append <file>

If I break the loop and examine the file, I see the initial contents are repeated over and over. Could anyone explain why this is happening?

I would have expected the contents of the file to be repeated twice not that it goes into a loop.

like image 351
Rakhesh Sasidharan Avatar asked Oct 17 '25 03:10

Rakhesh Sasidharan


1 Answers

Get-Content gives you content line by line, Append adds 1 line from the pipeline to the end of file. So for every line you encounter, a line gets added to the end. So - it never ends. If you expect to have your contents repeated twice, read the whole file into a variable, append it to itself, and then output your results to a file.

like image 61
Neolisk Avatar answered Oct 20 '25 13:10

Neolisk