Multiple foreground colors in PowerShell in one command

powershell change color of output text
powershell write-output color
powershell colors not working
powershell read-host color
powershell color menu
powershell text background color
what do the colors mean in powershell
powershell write-host format

I want to output many different foreground colors with one statement.

PS C:\> Write-Host "Red" -ForegroundColor Red
Red

This output is red.

PS C:\> Write-Host "Blue" -ForegroundColor Blue
Blue

This output is blue.

PS C:\> Write-Host "Red", "Blue" -ForegroundColor Red, Blue
Red Blue

This output is magenta, but I want the color to be red for the word red, and blue for the word blue via the one command. How can I do that?

PowerShell - How to format Write-Host with multiple colors, Since Write-Host only supports a single ForegroundColor -- I wrote this. can't rewrite it but use a switch statement to get the foreground color name instead and​  Muliple Foreground Colors in Powershell in One Command including spaces. I wanted to make a nice GUI for my little script. I've this post describing a function how to do it. It generally works fine but it doesn't like when there are spaces.

You could roll your own Write-Color command or something that looks for inline tokens that change the color. This is how ANSI escape sequences used to work back in the BBS days.

But you could achieve what you want by doing:

Write-Host "Red " -f red -nonewline; Write-Host "Blue " -f blue;

Here's a simple little function that does what you asked.

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
    for ($i = 0; $i -lt $Text.Length; $i++) {
        Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine
    }
    Write-Host
}

Write-Color -Text Red,White,Blue -Color Red,White,Blue

Write-Host, You can change the PowerShell console colors of the PSReadLine module that To get an overview of your current color settings, you can use this command: For instance, if you want to change the CommandForegroundColor to white, you 2020 (first Tuesday of the month, Office Patchday), Microsoft released several  In this post, we are going to see how to change the PowerShell colors. There are not many options when it comes to changing the PowerShell colors. We can see the available list of colors using the following script. Jump to: List of Colors in Powershell. Set PowerShell Colors. Verdict. Invoke-Command on Remote Computer PowerShell.

This function provides different syntactic sugar:

function color-Write
{
    # DO NOT SPECIFY param(...)
    #    we parse colors ourselves.

    $allColors = ("-Black",   "-DarkBlue","-DarkGreen","-DarkCyan","-DarkRed","-DarkMagenta","-DarkYellow","-Gray",
                  "-Darkgray","-Blue",    "-Green",    "-Cyan",    "-Red",    "-Magenta",    "-Yellow",    "-White")
    $foreground = (Get-Host).UI.RawUI.ForegroundColor # current foreground
    $color = $foreground
    [bool]$nonewline = $false
    $sofar = ""
    $total = ""

    foreach($arg in $args)
    {
        if ($arg -eq "-nonewline") { $nonewline = $true }
        elseif ($arg -eq "-foreground")
        {
            if ($sofar) { Write-Host $sofar -foreground $color -nonewline }
            $color = $foregrnd
            $sofar = ""
        }
        elseif ($allColors -contains $arg)
        {
            if ($sofar) { Write-Host $sofar -foreground $color -nonewline }
            $color = $arg.substring(1)
            $sofar = ""
        }
        else
        {
            $sofar += "$arg "
            $total += "$arg "
        }
    }
    # last bit done special
    if (!$nonewline)
    {
        Write-Host $sofar -foreground $color
    }
    elseif($sofar)
    {
        Write-Host $sofar -foreground $color -nonewline
    }
}

Examples:

color-Write This is normal text
color-Write Normal -Red Red -White White -Blue Blue -ForeGround Normal

Write-Color - Multiple colors on a single line! : PowerShell, does not take it as an object instead, it considers it as a name parameter or value and simply displays output in the console. Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.

Here is small a function I wrote to output colored text (it is actually smaller, but I rewrote it to be more understandable):

function Write-Color() {
    Param (
        [string] $text = $(Write-Error "You must specify some text"),
        [switch] $NoNewLine = $false
    )

    $startColor = $host.UI.RawUI.ForegroundColor;

    $text.Split( [char]"{", [char]"}" ) | ForEach-Object { $i = 0; } {
        if ($i % 2 -eq 0) {
            Write-Host $_ -NoNewline;
        } else {
            if ($_ -in [enum]::GetNames("ConsoleColor")) {
                $host.UI.RawUI.ForegroundColor = ($_ -as [System.ConsoleColor]);
            }
        }

        $i++;
    }

    if (!$NoNewLine) {
        Write-Host;
    }
    $host.UI.RawUI.ForegroundColor = $startColor;
}

It's quite simple to use: just use Write-Color "your text" and add some color name between curly brackets where you want the text to be colored.

Examples:

`Write-Color "Hello, {red}my dear {green}friend !"` will output

Script screenshot

You can put it in your $profile file to use it in a simple PowerShell prompt, or just add it to some scripts.

Change PowerShell console syntax highlighting colors of , What is the difference between write host and write output in PowerShell? While it's usually good enough for most scripts sometimes formatting one line of script with multiple colors is required. Wouldn't it fun to have Green Red Yellow outputed by PowerShell script? You can actually do that with 3 “simple lines.

This works too...

Write-Host "Don't forget to " -ForegroundColor Yellow -NoNewline; Write-Host "CALL YOUR MOM " -ForegroundColor Red -NoNewline; Write-Host "every day!" -ForegroundColor Yellow

Dark theme and tabs for PowerShell, , on the other hand, writes to the pipeline, so the next command can accept it as its input. Our new foreground and background colors in the PowerShell console. (Image Credit: Jeff Hicks) Again, you’ll need to insert the commands into your profile if you want this to be a persistent change.

Powershell Write-Host, RawUI.ForegroundColor = "White". (Although these commands wrap here, you'd enter each command on one line in the PowerShell console.)  This command displays the string "Red on white text.". The text is 'red', as defined by the ForegroundColor parameter. The background is 'white', as defined by the BackgroundColor parameter. These commands effectively suppress output of the Write-Host cmdlet.

PowerShell difference between Write-Host and Write-Output , He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. One of the questions I received is concerned with how to change the color you can easily put the commands in your PowerShell profile script to run Our new foreground and background colors in the PowerShell console. Or if you want to change color of the cmdlets, the TokenKind is Command. The latter corresponds to CommandForegroundColor in the screenshot above. These are the supported TokenKind parameters: None, Comment, Keyword, String, Operator, Variable, Command, Parameter, Type, Number, and Member.

Take Control of the PowerShell Console's Colors, Bored with the same old blue Windows Power Shell window? While there have been many changes made to the software itself, the one thing that has stayed Change Background and Foreground Colors in Windows 10 Use Simple Commands To Change Windows PowerShell Colors in Windows 10. DESCRIPTION In addition to accepting a default foreground and background color, you can embed one or more color specifications in the string to write, using the following syntax: #<fgcolor>[:<bgcolor>]#<text># <fgcolor> and <bgcolor> must be valid [ConsoleColor] values, such as 'green' or 'white' (case does not matter).

Comments
  • Asked this question 5 years ago and it was answered now. Very nice! Hat tip @Josh for the assist.
  • This function needs to be posted to connect.microsoft.com/PowerShell to get its functionality into PowerShell!
  • Now with background colors? Nice!
  • @Nixphoe glad you like it. I am currently thinking on few additional features but not sure it would get any need. I wanted to add ability to add level under which the message is displayed or not. For example you set it to level 1,2,3,4,5 and so on and depending on what level you tell it to display on the beginning of the script it would display just that. This could come useful in situations where you want to only get output from certain places, and then get output all of it when debugging.
  • @StanTastic that's fine, but it's better to copy the function from GitHub, as I fix/add new stuff there - github.com/EvotecIT/PSWriteColor/blob/master/Public/…
  • That function is very interesting. I think I'm going to dissect it and change it around so that it will use ECMA-48 color codes :).
  • Unfortunately PowerShell only supports the 16 basic ConsoleColor enumeration values. Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White
  • You could shorten -nonewline to -n and the colour name to the shortest resolvable prefix:- Write-Host "Red " -f r -n; Write-Host "Blue " -f Blu -n; Write-Host "Cyan " -f C
  • While this function is interesting, its name does not reflects the powershell naming convention : Verb-Subject. Check Approved Versbs for Windows PowerShell to check standard verbs.
  • What is the difference between white and White?