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.

3
  • Possible duplicate of UTF-8 output from PowerShell Commented Nov 30, 2015 at 9:17
  • No, it isn't. See clarification. Commented Nov 30, 2015 at 9:20
  • 1
    @zmbq according to the documentation, using Out-File -Encoding is the way to go, you cannot change the behavior of the redirection operators Commented Nov 30, 2015 at 9:59

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.