# PowerShell – How to format Write-Host with multiple colors

When you begin to write PowerShell scripts you usually just want it to do some simple tasks and automations. But…

blog

09 Apr 2016 de

![PowerShell – How to format Write-Host with multiple colors](/wp-content/uploads/2016/04/2017-06-29-22_33_11-Remote-Desktop-Manager.png)

[Bilder ansehen (6)](#pf-content-image-1-0)

When you begin to write PowerShell scripts you usually just want it to do some simple tasks and automations. But after a while some scripts grow so large that you actually want to have them output in a bit nicer way.

> Please notice this article **contains parts of information **(still useful) and **may not reflect all functionalities of this module**. For download, source code and so on you should refer to the dedicated **[PSWriteColor](https://evotec.xyz/hub/scripts/pswritecolor/) **module page. After reading this one… of course! It contains useful *information*, *examples *and *know-how*.

### ❗ Problem Description

Usually to output information in **PowerShell** we use **Write-Host.** By using parameters **ForegroundColor** and **BackgroundColor** parameters you can define nice looking output text.

```plain
Write-Host "Whole text is in green" -ForegroundColor Green
Write-Host "Whole text is in red" -ForegroundColor Red
Write-Host "Whole text is in white" -ForegroundColor White

Write-Host "Whole text is in red with background Yellow" -ForegroundColor Red -BackgroundColor Yellow
Write-Host "Whole text is in yellow with background Dark Green" -ForegroundColor Yellow -BackgroundColor DarkGreen
```

[!\[Two Write-Host examples showing colored text and background output\](/wp-content/uploads/2016/04/img_57092a48e101f.png)](/wp-content/uploads/2016/04/img_57092a48e101f.png)

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.

```plain
Write-Host "Green " -ForegroundColor Green -NoNewline;
Write-Host "Red " -ForegroundColor Red -NoNewline;
Write-Host "Yellow " -ForegroundColor Yellow -NoNewline;
```

Or even have it as one-liner:

```plain
Write-Host "Green " -ForegroundColor Green -NoNewline; Write-Host "Red " -ForegroundColor Red -NoNewline; Write-Host "Yellow " -ForegroundColor Yellow -NoNewline;
```

[!\[One-line Write-Host commands outputting green, red, and yellow text\](/wp-content/uploads/2016/04/img_57092a612dd7d.png)](/wp-content/uploads/2016/04/img_57092a612dd7d.png)

While this works it makes code very hard to read!

### ✅ Solution

But there's actually a nice and easy way to make it work without much effort. By adding [**Write-Color**](https://evotec.xyz/hub/scripts/pswritecolor/) function you give yourself couple of options. For example:

- Multiple colors in single line, if it's short enough

```plain
Write-Color -Text "Red ", "Green ", "Yellow " -Color Red,Green,Yellow
```

- Or if it's a bit longer…

```plain
Write-Color -Text "This is text in Green ",
                   "followed by red ",
                   "and then we have Magenta... ",
                   "isn't it fun? ",
                   "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan
```

And the colorful output from this PowerShell method:

![Write-Color output with green, red, magenta, white, and dark cyan text](/wp-content/uploads/2016/04/img_57092c34a499f.png)

This method actually has couple of other features. It allows easy adding of tabbing, lines before and after output. It's useful in creating menu's where it doesn't clutter the output too much. You can for example create:

- Options menu

[!\[Write-Color options menu with numbered choices\](/wp-content/uploads/2018/04/img_5acb7099e0203.png)](/wp-content/uploads/2018/04/img_5acb7099e0203.png)

```plain
Write-Color "1. ", "Option 1" -Color Yellow, Green
Write-Color "2. ", "Option 2" -Color Yellow, Green
Write-Color "3. ", "Option 3" -Color Yellow, Green
Write-Color "4. ", "Option 4" -Color Yellow, Green
Write-Color "9. ", "Press 9 to exit" -Color Yellow, Gray -LinesBefore 1
```

- Or adding a tab before your line

```plain
Write-Color -Text "This is text in Green ",
                "followed by red ",
                "and then we have Magenta... ",
                "isn't it fun? ",
                "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan -StartTab 3 -LinesBefore 1 -LinesAfter 1
```

![Write-Color output indented with a three-tab offset](/wp-content/uploads/2018/04/img_5acb70ae7a935.png)

- Or adding 2 lines, with 2 empty lines before it

![Write-Color output with two blank lines before the text](/wp-content/uploads/2018/04/img_5acb70c1ef993.png)

```plain
Write-Color -LinesBefore 2 -Text "This little ","message is ", "written to log ", "file as well." -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt" -TimeFormat "yyyy-MM-dd HH:mm:ss"
Write-Color -Text "This can get ","handy if ", "want to display things, and log actions to file ", "at the same time." -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt"
```

And the output of final script:

```plain
Write-Color -Text "Red ", "Green ", "Yellow " -Color Red,Green,Yellow

Write-Color -Text "This is text in Green ",
                "followed by red ",
                "and then we have Magenta... ",
                "isn't it fun? ",
                "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan

Write-Color -Text "This is text in Green ",
                "followed by red ",
                "and then we have Magenta... ",
                "isn't it fun? ",
                "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan -StartTab 3 -LinesBefore 1 -LinesAfter 1

Write-Color "1. ", "Option 1" -Color Yellow, Green
Write-Color "2. ", "Option 2" -Color Yellow, Green
Write-Color "3. ", "Option 3" -Color Yellow, Green
Write-Color "4. ", "Option 4" -Color Yellow, Green
Write-Color "9. ", "Press 9 to exit" -Color Yellow, Gray -LinesBefore 1

Write-Color -LinesBefore 2 -Text "This little ","message is ", "written to log ", "file as well." `
            -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt" -TimeFormat "yyyy-MM-dd HH:mm:ss"
Write-Color -Text "This can get ","handy if ", "want to display things, and log actions to file ", "at the same time." `
            -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt"
```

[!\[Write-Color\](/wp-content/uploads/2016/04/2017-06-29-22_33_11-Remote-Desktop-Manager.png)](/wp-content/uploads/2016/04/2017-06-29-22_33_11-Remote-Desktop-Manager.png)

And log file data properly written to file (it's additive so if you run script multiple times it will not overwrite the log):

[!\[PowerShell ConsoleColor output listing available colors\](/wp-content/uploads/2017/06/img_59556497be689.png)](/wp-content/uploads/2017/06/img_59556497be689.png)

### 📝 Notes

You can find the colors you can use by using simple code:

```plain
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -ForegroundColor $_ }
```

Obviously **black **is missing 🙂

[!\[PowerShell ConsoleColor list showing colors and omitting black\](/wp-content/uploads/2016/04/img_5709307d7ee74.png)](/wp-content/uploads/2016/04/img_5709307d7ee74.png)

## Zugehörige Projekte

Projekte, die zu Tags, Kategorien und Inhalt dieses Beitrags passen.

[Alle Projekte](/de/projekte/)

Projekte werden abgeglichen...

[Zurück zum Blog](/blog)

## Ähnliche Beiträge

- [Write-Color is now published as PowerShell Module](/de/blog/write-color-is-now-published-as-powershell-module/)
- [Write-Color – 2 year Anniversary Edition – v0.3](/de/blog/write-color-2-year-anniversary-edition-version-0-3/)
- [Submitting blogs to web.archive.org using PowerShell](/de/blog/submitting-blogs-to-web-archive-org-using-powershell/)

<!-- PowerForge markdown artifact sha256:AC0C19A616703D0B95F2AEBB6AAA25F6DE8BBBFE3F2B89D7E49924207F44EBDC -->
