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.
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.
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