2

I have git setup to execute a powershell script during merges. I use Git Bash on windows for git commands. Powershell's Write-Host function outputs text to the Bash console, but using -ForegroundColor flag does not change the text color. Is there a way for the Powershell script to set the color of it's output in the Bash console?

1 Answer 1

2

First thing first.

Using Write-Host has been a hotly debated topic for a long while. Even by the inventor / author of PowerShell.

Write-Host Considered Harmful - by PowerShell founder Jeffrey Snover

https://jsnover.com/blog/2013/12/07/write-host-considered-harmful

When you are writing or reviewing PowerShell scripts, I’d like you to remember the following rule of thumb:

Using Write-Host is almost always wrong.

Write-Host is almost always the wrong thing to do because it interferes with automation. There are typically two reasons why people use Write-Host:

Lot's of other articles exist on the topic.

In earlier versions of PoSH, Write-Host could not be used in pipeline, as the moment yo use it the data is gone from the buffer.

However, in PoSHv5 Jeffrey Snover now says...

With PowerShell v5 Write-Host no longer "kills puppies". data is captured into info stream

https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1

Description

The Write-Information cmdlet specifies how Windows PowerShell handles information stream data for a command.

Windows PowerShell 5.0 introduces a new, structured information stream (number 6 in Windows PowerShell streams) that you can use to transmit structured data between a script and its callers (or hosting environment). Write-Information lets you add an informational message to the stream, and specify how Windows PowerShell handles information stream data for a command.

Colorized text to the screen for user interaction stuff - even that can be worked around easily.

Write-Verbose has a blue color Write-Warning has a orange color

You can still use colors, without using Write-Host, by doing this ...

PowerTip: Write PowerShell Output in Color Without Using Write-Host

https://blogs.technet.microsoft.com/heyscriptingguy/2012/10/11/powertip-write-powershell-output-in-color-without-using-write-host

Summary: Write colorized output to the Windows PowerShell console without using the Write-Host cmdlet.

Secondly, you also don't state what editor / console you are using. powershell.exe/powershell_ise.exe or something else, say VSCode.

Now, full disclosure: I not tried this in Bash. Yet, the without spending the extra time configuring PoSH for Bash integration...

Configuring the PowerShell ISE for use with Git and GitHub

The goal of this blog article is to configure the PowerShell ISE (Integrated Scripting Environment) so the management of Git version control can be performed from it. Most tutorials you’ll find will attempt to lead you down the path of using SSH instead of HTTPS to synchronize your repositories to GitHub from the command-line but that’s really over-complicated and unnecessary if you’re using a Windows based machine.

http://mikefrobbins.com/2016/02/09/configuring-the-powershell-ise-for-use-with-git-and-github

...then, bash terminal and PoSH console are two different environments. If you are saying, that you are using Bash command in a PoSH script and redirecting output to the Bash terminal, then I get that, but, outside of using Write-Host being wrong in most cases, when PoSH sends output to something else, then it's the destination that controls how it will look, not PowerShell.

Even in using PoSH WinForm / WPF, from text boxes can be sent text from PoSH, but you must use the from properties to set colors.

Anyway, there are several discussions on color in GIT Bash with PowerShell. See if these help further.

Using Git from PowerShell

Customize your Git environment

You may want to customize some of the settings of your Git environment, especially if this is a new install of Git. Being a good project contributor in Git you should identify yourself so that Git knows who to blame for your commits. Also, I found that the default colors used by Git in the shell could be hard to read. So I customized the colors to make them more visible. For more information, see the Customizing Git topic in the Git documentation.

The following commands only need to be run once. You are setting global preferences so, once they are set, they are used every time you start a new shell.

https://seanonit.wordpress.com/2016/12/05/using-git-from-powershell

Setting colors for ls in git bash on windows

I have installed GitHub for Windows recently and am using the git bash prompt - the one thing that is bugging me right now is when I type LS all directories are listed in blue.

How do I change my git bash shell so that when I type LS the directories are listed in a different color to blue?

https://stackoverflow.com/questions/14049896/setting-colors-for-ls-in-git-bash-on-windows

How do you change the color scheme in bash on Ubuntu for Windows?

I've tried to create color schemes with https://terminal.sexy and http://ciembor.github.io/4bit/ but I can't get any of their exports to work with bash on Ubuntu on Windows.

What's the correct approach to customize the colors in boUoW?

How do you change the color scheme in bash on Ubuntu for Windows?

1
  • Thank you for your answer! I have indeed decided to start using PoSH in which case all the standard ways you mentioned of changing text color work in the main powershell console when called through git hooks etc.
    – Geordie
    Mar 15, 2018 at 0:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .