Colors! Magical colors!

PowerShell with colors! <3

When you don’t need to pass the stuff around and down the pipeline, Write-Host is an awesome command. It has all the shiny colors! ^_^

The question I was asked yesterday was: How do I see all PowerShell color combinations?

As it turns out, it’s very simple 🙂

The explanation may not be as simple, but I’m good and will ease you in. Let’s try the basic coloring of our output.

Did you like it?

Of course not. Blue on blue is stupid. And prolonged exposure to such madness can lead to blindness.

I could probably make a fortune writing songs.

Let’s try this one, with the background color changed as well:

It’s a miracle! I can see!

Okay, a quick side note – You can refer to colors using only a few letters from their names.

This will get you Yellow and Red output. You can use Yell or Re if you want, too. You cannot use B or Bl as those are shared among Blue and Black, but Blu and Bla are free.

A word of warning. Just because you can do this doesn’t mean you should! The code looks retarded and other people reviewing it will think the same of you.

Use full color names or, if you have to, their numbers.

So, how do you get all the full color names?

Let’s ask PowerShell for help. Use the non-existent color and observe the error:

Line 5 gives us all the colors. Line 3 gives us something better – System.ConsoleColor type!

Let’s enumerate all the values:

There you go. You can programmatically access them now. Or, even better, if you remember I mentioned addressing colors by their numbers… 🙂

Yes, let’s make a function!

I won’t go into details here. It’s pretty self-explanatory. Get all colors and sort them out in a nice way. Execute!

Of course, the right side will look much nicer in your console. Damn WordPress. Need to jump through hoops to get some colors in here. Well, just run the function and check out the results.

This gets us only halfway there, though. What about background colors?

I know what you’re thinking! “Let’s make a matrix!”

Just in case you weren’t thinking “OMG! YES! A MATRIX!” know that I’m deeply disappointed in you. So better nod your heads to assure me that matrix was totally what you were thinking of.

A matrix, then!

Much more easy to figure out that the first function, right?

We have two loops. Inner for colors and outer for backgrounds. When we mix them up, the magic happens! Rabbits and unicorns get together to gaze at the sheer beautifulness of this rainbow!

Not useful as a Rainbow Table, but it’s useful as a color chart. Pick the foreground/background color numbers you like and use the first function to figure out the color names for these numbers.

Dot-source the file and both functions are ther. Even better, add them to your PowerShell $PROFILE for getting easy access to colors in every PowerShell session.

For the uninitiated, you can access your PowerShell profile with notepad $PROFILE. For more advanced uninitiated, run notepad++ $PROFILE.

Paste functions in the file, save it and restart PowerShell session. Thank me later. Or buy me a beer. Or thank me later and buy me a beer. Or get me a premium Grammarly. Or all three. You can get me Amazon Gift card, too. Or Google Play Store.

Hm. What if I just give you my IBAN? 😉

Check the screenshot first and decide how many zeros will you add 🙂

Copy the script, or grab from GitHub – https://github.com/PowerSix/MyPowerShellSpace.

Leave a Comment