1

In Powershell using > is the same as using | Out-File, so I can write

"something" > file.txt and It will write 'something' into file.txt . This is what I expect of a shell. Unfortunately, Powershell uses Unicode for writing file.txt. The only way to change it into UTF-8 is to write the quite long command:

"something" | Out-File file.txt -Encoding UTF8 

I want to override the > shortcut, so that it adds the UTF-8 encoding by default. Is there a way to do that?

NOT A DUPLICATE CLARIFICATION:

This is not a duplicate. As is explained clearly here, Out-File has a hard-coded default. I don't want to change Out-File's behavior, I want to change >'s behavior.

Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171

2 Answers2

3

No, can't be done

Even the documentation alludes to this. From the last paragraph of Get-Help about_Redirection:

When you are writing to files, the redirection operators use Unicode encoding. If the file has a different encoding, the output might not be formatted correctly. To redirect content to non-Unicode files, use the Out-File cmdlet with its Encoding parameter.

(emphasis added)

Community
  • 1
  • 1
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
1

The output encoding can be overriden by changing the $OutputEncoding variable. However, that only works for piping output into executables. It doesn't work for redirection operators. If you need a specific encoding for file output you must use Out-File or Set-Content with the -Encoding parameter (or a StreamWriter).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328