3

When executing directly, everything works as expected:

& git status

# OK: "modified:   awél.txt"

Storing in a variable

$aargh = & git status
$aargh

# NOT OK: "modified:   aw├®l.txt"

How can I tell it what encoding to use?

1 Answer 1

3

Character-encoding issues usually don't surface if you print an external program's output to the screen, but they may if you capture it or send it to another command via the pipeline, because PowerShell decodes the output into .NET strings in the process.

In order for PowerShell to recognize git's output correctly, you must:

  • know what character encoding git uses in its output

  • and set [Console]::OutputEncoding to that encoding (on Windows, it defaults to your system's legacy OEM code page).

git uses UTF-8, so you must run the following before capturing git's output:

[Console]::OutputEncoding = [System.Text.Encoding]::Utf8

Note that whether or not you [need to] use & to call an external program makes no difference with respect to character-encoding issues.

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

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.