Calling Git from Powershell

Git uses UTF-8 by default in its output, but Powershell uses typically UTF-16 Little Endian. So if you try to get information from the Git command line directly, you will run into encoding issues.

You can however tell Powershell to decode the Git output using [Console]::OutputEncoding. Here is the code I use:

function rungit () {
    $repopath = $args[0]
    $gitargs = $args[1..($args.count-1)]
    cd $repopath
    $enc = [Console]::OutputEncoding
    # Git uses UTF-8 encoding, but Powershell expects UTF-16 by default
    # Let's switch temporarily to UTF-8
    # (against expectation, OutputEncoding defines also the encoding of the stdout we read)
    [Console]::OutputEncoding = [text.encoding]::utf8
    Write-Host "Calling: " $GIT $gitargs
    $output=(&$GIT $gitargs) | Out-String
    [Console]::OutputEncoding = $enc
    if ($LastExitCode -ne 0) {
        Write-Host $output
        error "Git command failed: $GIT $gitargs (in path: $repopath)"
    return $output
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s