Write-Output Gets Foreground and Background Colors and More

Every once in a while a forum post comes along that really captures my interest. Take this one for instance.

In this post, the creator wanted to build out a start up menu in the ConsoleHost that has a solid white line, another white line below that, with the words “PowerShell Project 1” in it, and a final white line beneath that. That might be difficult to imagine, so here’s a visual representation, and then what his/hers looked like. I had recommended to use a here-string to fix the problem, but unfortunately we didn’t have the same results.

Note: While you can’t see them, in both this and the below image, there are spaces on the lines above, below, and before and after “PowerShell Project 1.” This is how we can ensure the white rectangle around the text.


I can’t say for certain why theirs rendered differently, but I suspect version, or PSReadline, or possibly console font/size. That wasn’t important; what was, was trying to figure out a way around this problematic inconsistency. We have the option to change the background color of the area where we type, and so I wondered if I could temporarily do that inside the ConsoleHost. It turns out I could. With that new piece of knowledge, I set out to write a wrapper function around Write-Output. In my version — Write-TMOutput — it includes -ForegroundColor and -BackgroundColor parameters with an option to horizontally and vertically pad the text (object).

First, here’s a few images to show my new function in action. This first image shows the commands included in the TMOutput module.


The next example image shows some basic, Write-Output type usage. One where we pipe to the function, and one where we don’t. Fairly standard.


Now, the next example is where this starts to get fun: the incorporation of the -ForegroundColor and -BackgroundColor parameters. Prior to running these commands, and those in the last two examples, I removed the PSReadLine module so the colors would be easier to spot.


Next is a demo of two additional parameters: -HorizontalPad and -VerticalPad. These will allow you to add spaces before and after the value supplied to the -InputObject parameter (the text), and add lines above and below the text. This is the part that gets back to the request in the Microsoft Technet forum post. I should mention that my solution in the forum post would be different now that I’ve spend some time writing this function — there are better ways to do things that I hadn’t considered at the time I replied to the post.


The next example shows some usage with the Get-ADUser cmdlet. Keep in mind that if you combine this function with other cmdlets, that the -HorizontalPad and -VerticalPad parameters cannot be used. In my mind, they’re just a bonus to the option of using colors with Write-Output. I should mention it now, but this function was intentionally written to only work in the ConsoleHost. Perhaps I’ll add the option of using it in the ISE in the future.


I’ve written an additional function that will allow you to quickly see the available colors from which you can choose. It’s called Show-TMOutputColor, and produces the results below. This might come in handy as you’re using the modified Write-Output.


Now, on to the module. It’s the first time I’ve done it, but I’ve uploaded this to the PowerShell Gallery. I believe publishing to the PowerShell Gallery was one of my PowerShell New Year’s Resolutions for 2016, so there’s one down! You can use this direct link or, one better, download the module from your console, using this command:

Install-Module -Name TMOutput

Thanks for reading this post. Hopefully these function will be a useful way to get around the need for Write-Host’s foreground and background color options while working in the console. That, and maybe the TechNet thread creator can do whatever they wanted, too.

7 thoughts on “Write-Output Gets Foreground and Background Colors and More

    1. tommymaynard Post author

      Hi Cody,

      Unfortunately, I suppose, I stopped at getting it to work in the ConsoleHost. While the ISE might be an option, it wasn’t included on my first pass. I played with the options there briefly, but it simply didn’t respond as well. :\

  1. Adam

    Hello There,

    What do I have to do in order to get the PowerShell module to work as the below command didn’t work for me?

    Install-Module -Name TMOutput

    Please advise.



      1. tommymaynard Post author

        To download the module, you need to get to a computer that has, or includes, the PowerShellGet module. Here’s how you can check for that module: Get-Module -Name PowerShellGet -ListAvailable. If it’s there, the above Install-Module command should, or should have, worked. If you don’t have it, you’ll need to find a computer that does, and use the Save-Module command on the project’s page on the PowerShell Gallery: https://www.powershellgallery.com/packages/TMOutput/1.1. With that, you could save and then copy it over the computer that doesn’t have that PowerShellGet module. It would need to be copies to C:\Program Files\WindowsPowerShell\Modules, or C:\Users\\Documents\WindowsPowerShell\Modules.

        1. Adam

          I don’t know of a computer that has the TMOutput module. Can you E-Mail me a saved module so that I can install it on my computer please.

          Thanks – Adam


Leave a Reply

Your email address will not be published.