Zum Inhalt

Monat: Juni 2012

Powershell – Colorize string output with colorvariables in the output string

The other day I saw a script output that was colored with magenta. I was a bit impressed by the readability of the output messages. As I looked to the source code I saw something (ugly) like:

The output looked like: The Output is not so bad, but the effort is enormous. I came to the idea to encode the colorsettings within the output string. So I ended up with the following Script:

Now it’s possible to use a single string to encode the color by seperating each color with a hashtag (#). Here is an example: The syntax is very simple. Choose a color for the next characters and put it into hashtags (#). To stop coloring the characters just put another hashtag behind the colored characters to reset the foreground color to its default.

If you want to use any other charater, or command for the coloring, just replace the hashtag of the split function in line 20 with your character/command. I think this is a usefull function, because script output is often needed and coloring the important parts of the output is sometimes very helpful 🙂 Edit: I edited the script to work even…

Weiterlesen Powershell – Colorize string output with colorvariables in the output string