Support for `foregroundcolor` inside PS script

Aleksander Sjåfjell's Avatar

Aleksander Sjåfjell

24 Oct, 2016 10:16 AM

It seems that the flag `foregroundcolor` for PowerShell only works directly within a `- ps:` build script. Using the flag within a loaded powershell script file does not give colored output. I have tested with simple output like this: `Write-Host -foregroundcolor green "My successful message"`. Is there some magic to get this behaviour within a powershell script like `powershell .\MyScript.ps1`? All help appreciated.

  1. Support Staff 1 Posted by Ilya Finkelshte... on 24 Oct, 2016 08:22 PM

    Ilya Finkelshteyn's Avatar

    Hi Aleksander,

    You can call it this way to use .ps1 script and still have foregroundcolor displayed:

     ps: .\MyScript.ps1

    Will it work for you or for some reason you have to call it only from cmd?

    Thank you,

  2. 2 Posted by hubert.bukowski on 12 Jan, 2018 10:08 AM

    hubert.bukowski's Avatar

    I know that this thread is pretty old but I have similar problem.
    In my case I am using single cmd script for CI and for regular manual build. This script tries to find PowerShell Core in requested version (for example 6.0.0-rc.2) and installs it if not found. Then it runs ps1 script. In this scenario all output from pwsh.exe has default console color.

    For comparison Travis output:

  3. Support Staff 3 Posted by Ilya Finkelshte... on 12 Jan, 2018 10:37 PM

    Ilya Finkelshteyn's Avatar

    @hubert.bukowski unfortunately this is by design right now. We are adding PowerShell Core support so probably next week you can simple use - pwsh: prefix to run it as is. But it still will be colorless, sorry.

  4. 4 Posted by hubert.bukowski on 12 Jan, 2018 11:01 PM

    hubert.bukowski's Avatar

    Thank you for the response. For me it would be best to have full control over the version of pwsh installed so I will need to stay with cmd.
    Fortunately I found a workaround. I'm using ANSI escape sequences for colors when run on AppVeyor and regular -ForegroundColor otherwise:

  5. Support Staff 5 Posted by Ilya Finkelshte... on 12 Jan, 2018 11:08 PM

    Ilya Finkelshteyn's Avatar

    This is amazing! Thanks a lot for the idea :)

  6. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:26 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac