PowerShell Capture Git Output

Question:

Problem

I am running some git commands through PowerShell but I am having issues capturing the output.

Many of the ways I have looked into have failed to capture any output at all until I tried using Out-File . Unfortunately, despite how well this has been working for me, I have found that something is going wrong with the git push command that is preventing me from capturing the output…

My code looks as follows:

When I look at the log files, it shows everything but the output of the git push. So in troubleshooting this, I removed the | Out-File from the line and noticed that even the output window was not showing the output of the git push anymore. It was only when I completely removed the Out-File from all of the git commands that I was able to get the output to display as expected again. Unfortunately, this put me back at square one.

Question

Does anyone know how to best capture the output of git commands using PowerShell, or can point out what I am doing wrong?

Answer:

As I explained here, or here, Git command output are done on stderr, not stdout.

So in your case, you need to pipe stderr.
See “Redirection of standard and error output appending to the same log-file” for instance.

Note: with Git 2.16 (Q1 2018), you can try and set first

Then stderr should be redirected to stdout.

In “PowerShell – Capturing STDERR Output for Git Commands“, the OP Brandon points out to the --porcelain option of (for instance) git push:

It allows to convert the outputs into a machine readable format and pipe them to the output.

Source:

PowerShell Capture Git Output by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply