Powershell – The Colors and the Lights!

PowershellBIG

Powershell is lot a big box of Legos.   There are so many Cmdlets and Variables in the mix, and as you start play you discover something about them. That each piece has SO many more pieces already attached to it.  Because each Property can use several Methods and after Methods are applied you have new Properties to work with; sometimes OTHER properties buried within.

It’s a lot like that whole “mirror facing the mirror” bit except it produces more usefulness each and every time.

So lets look at a built in Variable that you might overlook all the time.  One that is a perfect example of pieces within Rabbit Hole.

 

$HOST

 

This is more than a variable.   It’s your connection under the hood to your console.   It not only tells you information about how the Date/Time is formatted but also locations of the cursor on the screen, what color it is.  As you dig into the methods you can call the very sequences that CmdLets call up

For example, the Cmdlet you call up to read input from the console READ-HOST, can also be expressed from the $HOST variable as

 

$HOST.UI.ReadLine()

 

More importantly to you, as somebody working in a Powershell environment, this is your method to adjust color and cursor position.   You can tell from this variable how BIG the actual console has been defined as (Columns by rows).   If you were to get creative, you could use this to write a VERY nice non GUI menu system.

 

$HOST.UI.RawUI contains some of the most important attributes of all.  These are the ones you’re going to want to play with a LOT if you get heavily into the console.  Just typing it will give you the list.

 

image

 

To read any one of these just put a “.” between the variable and the particular property.   Note.  Just like in many parts of Powershell, as you start opening up the little box more pieces show up.

So although typing

 

$HOST.UI.RawUI.WindowsTitle

 

Will show just my present Window Title “The Wild and Wonderful World of Sprinkles” (normally it’s “Windows Powershell” but as you can see I was playing already) typing in

 

$HOST.UI.Rawui.CursorPosition

 

image

 

Will show two more variable on the screen (In this case the Current Column and Row of the Cursor in the Console) and again just tack on a “.” to step deeper into the Rabbit Hole.

 

$HOST.UI.RawUI.CursorPosition.X

$HOST.UI.RawUI.CursorPosition.Y

 

And to SET any of these values just simply assign using the normal methods

 

$HOST.UI.RawUI.CursorPosition.X=10

Of course in the live console, it’s a little useless since the minute you type in this command it will reset back to a new location.  But within a script where you want to output data to a user or Put a Warning at the middle of the console, this is where it gains power.   Colors are even easier!

 

$HOST.UI.RawUI.ForegroundColor

$HOST.UI.RawUI.BackgroundColor

 

Will show you what it is, and to change it, just assign either one a string value from the assigned list; "DarkRed","DarkBlue","DarkCyan","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow" or "White".

 

$HOST.UI.RawUI.ForegroundColor=”Yellow”

 

Has now just changed the Cursor and typing letters to Yellow.   It will look weird with the new letters one color and the old letters another until you execute a CLEAR-HOST.

But as I said, this is a big box of “Legos” inside.  Poke about, have fun.  Discover what YOU can do with Powershell.

 

It’s never the same for two people.

Sean
The Energized Tech

Leave a Reply