'echo .filetoignore >> .gitignore adds spaces between each character in Powershell

I'm trying to add a line to my .gitignore file from Powershell.

When I run this in git bash or command prompt:

echo .filetoignore >> .gitignore

it correctly adds this line to my .gitignore file:

.filetoignore

when I run the same command from Powershell, it adds a space between every character:

. f i l e t o i g n o r e

What is the proper way to perform this operation in Powershell and add this line to .gitignore?

.filetoignore



Solution 1:[1]

It is encoding problem. Windows internally works with strings in 2 byte encoding always.

Avoid using >> in powershell, use Add-Content, or Out-File -Append, or [IO.File]::AppendAllLines, or [IO.File]::AppendText, where you can specify encoding used.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 filimonic