Tag Archives: Write-TMOutput

I am TechNet Gallery Years Old, Part II

It is time for Part II of this series of posts. If you did not read the first one, I would recommend you start there and cover the first four posts, which are crossed out below. Additionally, you will pick up some additional information about this project. Time to cover the next three.

5.”Specify and Create Multiple Credential Objects”

This was a fun project and like most of these TechNet Gallery scripts/modules, I took the opportunity to write about them on my site, too. I wrote about this one twice (Part I | Part II), as I added a new feature between those. The GitHub Gist link to the newest version is included on both parts — images too!

6. “Check Email Address Domain Against the Top-Level Domain (TLD) List from IANA”

It turns out I did not write a post to go along with this script — that may be a first. First off, you can get a hold of this script by visiting this GitHub Gist. And second, let me tell you a little about this script… right after I reeducate myself about it. Turns out this is very old. Its purpose was to determine whether or not the domain (.com in the below instance), is a valid Top-Level Domain (TLD) or not.

Beyond returning that, it downloads a file from IANA, which it checks the domain against, can provide a bit more information using the MoreInfo parameter, and can pump out the variables it uses in the function using the ShowVars parameter. The Days parameter is used to determine whether the function should use the current file from IANA or download a new one. Neat ideas, but the function is showing its age.

7. “TMConsole Module”

This one was a big deal. It made its way to the PowerShell Gallery and as of today, it is practically 50 downloads away from 3,000. My goal was to add ForeGround and Background colors to Write-Output like Write-Host has always had. And, the best part, it actually worked. You can read more about it here, as well as obtain the information needed to download it. Let’s push it past 3,000 downloads! And, it is really using Write-Output! Written for Windows PowerShell and works in PowerShell. Here is an image from today.

And now, there is a Part III!

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.

write-output-gets-foreground-and-background-colors-and-more01
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.

write-output-gets-foreground-and-background-colors-and-more02

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.

write-output-gets-foreground-and-background-colors-and-more03

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.

write-output-gets-foreground-and-background-colors-and-more04

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.

write-output-gets-foreground-and-background-colors-and-more05

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.

write-output-gets-foreground-and-background-colors-and-more06

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.

write-output-gets-foreground-and-background-colors-and-more07

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.

write-output-gets-foreground-and-background-colors-and-more08

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.