Note: The scenario described below may not match the OP's. The answer may still be of interest if you find that file content prints as follows to the consoler after having used >>
to append to a preexisting file in Windows PowerShell; note what appears to be extra spacing and extra empty lines:

To avoid your problem, which most likely stems from an unintended mix of different character encodings in the output file produced by >>
, you have two options:
- If you do know the character encoding used for the preexisting content in the output file, use
Out-File -Append
and match that encoding via the -Encoding
parameter:
# Using UTF-8 in this example.
$logging | Format-Table -AutoSize -HideTableHeaders |
Out-File -Append -Encoding Utf8 $CWD\log.txt
Note that >
/ >>
are in effect like calling Out-File
/ Out-File -Append
, except that you don't get to control the character encoding.
# Add-Content, unlike >>, matches the character encoding of the existing file.
# Since Add-Content, unlike > / >> / Out-File, uses simple .ToString()
# stringification you first need a call to `Out-String`, which provides
# the same formatting that > / >> / Out-File implicitly does.
$logging | Format-Table -AutoSize -HideTableHeaders |
Out-String -Stream | Add-Content $CWD\log.txt
Read on for background information.
Assuming you're using Windows PowerShell rather than PowerShell [Core] v6+[1]:
The most likely cause (the explanation doesn't fully match the output in your question, but I suspect that is a posting artifact):
You had a preexisting log.txt
file with a single-byte character encoding[2], most likely either the legacy encoding based on your system's active ANSI code page or a UTF-8 encoded file (with or without a BOM).
When you appended content with >>
, PowerShell blindly used its default character encoding for >
/ >>
, which in Windows PowerShell[1] is "Unicode" (UTF-16LE), which is a double-byte encoding[2] - in effect (but not technically) these redirection operators are aliases for Out-File [-Append]
.
The result is that the newly appended text is misinterpreted when the file is later read, because the UTF-16LE characters are read byte by byte instead of being interpreted as the two-byte sequences that they are.
Since characters in the ASCII range have a NUL
byte as the 2nd byte in their 2-byte representation, reading the file byte byte sees an extra NUL
("`0"
) character after every original character.
On Windows[3], this has two effects when you print the file's content to the console with Get-Content
:
What appears to be a space character is inserted between ASCII-range character so that, say, foo
prints as f o o
- in reality, these are the extra NUL
characters.
An extra, (apparently) empty line is inserted after every line, which is a side effect of PowerShell accepting different newline styles interchangeably (CRLF, LF, CR):
Due to the extra NUL
s, the original CRLF sequence ("`r`n"
) is read as "`r`0`n`0"
, which causes PowerShell to treat "`r"
and "`n"
individually as newlines (line breaks), resulting in the extra line.
Note that the extra line effectively contains a single NUL
, and that the subsequent line then starts with a NUL
(the trailing one from the "`n"
), so among the misinterpreted lines all but the first one appear to start with a space.
[1] PowerShell [Core] v6+ now consistently defaults to BOM-less UTF-8 across all cmdlets. While >>
(Out-File -Append
) still don't match an existing encoding, the prevalence of UTF-8 files makes this less of a problem. See this answer for more information about character encoding in PowerShell.
[2] Strictly speaking, UTF-8 and UTF-16 are variable-length encodings, because not every byte in UTF-8 is necessarily its own character (that only applies to chars. in the ASCII range), and, similarly, certain (exotic) characters require two 2-byte sequences in UTF-16. However, it is fair to say that UTF-8 / UTF-16 are single/double-byte-based.
[3] On Unix-like platforms (Linux, macOS) you may not even notice the problem when printing to the terminal, because their terminal emulators typically ignore NUL
s, and, due to LF ("`n"
) alone being used as newlines, no extra lines appear. Yet, the extra NUL
s are still present.