This is because write-output defaults to UTF-16 text encoding, which is 2 bytes per character. When you are dealing with text that fits into the ASCII codepage range, the 2nd byte of each character will be zero.
This is controlled by the $OutputEncoding
global variable, so you could set that to ASCII.
Another option is to use the cmdlet Out-File, which has an explicit encoding parameter. I would suggest you use this instead of output redirection, because that saves you from changing your environment globally (by setting the global preference variable $OutputEncoding
)
Using Out-File, and setting encoding to be ASCII, your example would look like this:
"abcd" | out-file "mytext.txt" -Encoding ASCII
Do be aware that not all characters are representable in ASCII, and you should determine whether this is an appropiate encoding for your purpose. Personally I would typically go for UTF-8, since it is ASCII equivalent when characters fall in the ASCII range from 0-127, but also handles international characters. Obligatory link about text encoding.