ReadAllText()
returns an empty string-object, and since you're using -Append
with your Out-File
cmdlet, the error.txt
is updated with this emtpy-string + a linebreak(because of -Append
) each time.
You can solve it by adding a check to test if the data from temp.txt
is not null. That will remove the empty string-object from being pipelined to Out-File
in the first place. Something like this:
[System.IO.File]::ReadAllText("D:\Temp.txt") | ? { $_ } | Out-File D:\Error.txt -Append -Encoding
or
[System.IO.File]::ReadAllText("D:\Temp.txt") | ? { $_ -ne "" } | Out-File D:\Error.txt -Append -Encoding