# 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 pl

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

[Zobacz obrazy (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
```

[!\[Dwa przykłady Write-Host z kolorowym tekstem i wyjściem na tle\](/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;
```

[!\[Jednolinijkowe polecenia Write-Host z zielonym, czerwonym i żółtym tekstem\](/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:

![Wyjście Write-Color z tekstem zielonym, czerwonym, magenta, białym i ciemnoturkusowym](/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

[!\[Menu opcji Write-Color z numerowanymi wyborami\](/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
```

![Wyjście Write-Color z wcięciem o trzy tabulatory](/wp-content/uploads/2018/04/img_5acb70ae7a935.png)

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

![Wyjście Write-Color z dwiema pustymi liniami przed tekstem](/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):

[!\[Lista ConsoleColor w PowerShellu z dostępnymi kolorami\](/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 🙂

[!\[Lista ConsoleColor w PowerShellu z kolorami i bez czarnego\](/wp-content/uploads/2016/04/img_5709307d7ee74.png)](/wp-content/uploads/2016/04/img_5709307d7ee74.png)

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

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