Powershell: After using Out-File, Robocopy will append a bunch of gibberish to the same log file
-
07-12-2019 - |
Question
I need to write to a particular log file, and append the results of multiple robocopy commands to the end of that file.
The trouble is, when the robocopy is preceded by an out-file command, the robocopy writes a bunch of random characters to the log. My guess is that the log is still in use by the previous out-file command, but from what I could find about it, out-file is supposed to create, open, and close files automatically.
Here is a slimmed down example of what I'm trying to do:
"lala" | Out-File -filepath log.txt
robocopy .\source .\destination 1.txt /log+:log.txt
In the above example, the log is created, and the contents look like this:
lala ⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭഭ †佒佂佃奐††㨠›††潒畢瑳䘠汩潃祰映牯圠湩潤>獷†††††††††††††††ⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭⴭഭഊ 匠慴瑲摥㨠䴠湯䴠牡>
etc.
Commenting out the first line and using only the robocopy command works just fine, but if I add the first line, the log gets messed up again. Anyone have any ideas/wisdom for me?
Thanks for reading. d:- D
Solution
Just encode in the same manner of robocopy log:
"lala" | Out-File -filepath log.txt -encoding utf8 -append
then you can append robocopy logs.