PowerShell Profile Tricks – Random Background Color

The need:  To run multiple PowerShell windows and keep it straight which one is working on what.

Example:

  1. In PowerShell window #1 you’re working with SharePoint
  2. In PowerShell window #2 you’re working with Active Directory users
  3. In PowerShell window #3 you’re goofing off customizing your profile so you can write a blog post

Only problem is, once you’re working with these windows, it’s hard to tell which window was the one you are working on your profile in – because they all look like this:

image image image

Here’s how to alleviate this problem automatically using an awesome PowerShell profile trick

Open up your PowerShell profile (or create a PowerShell profile if you haven’t already).

Add this function to your profile (and then call it right from your profile):

function random-ui {
    $random = New-Object System.Random
    switch ($random.Next(5)) {
        0 {$host.ui.RawUI.BackgroundColor = "DarkMagenta"; $host.ui.RawUI.ForegroundColor = "White"}
        1 {$host.ui.RawUI.BackgroundColor = "Black"; $host.ui.RawUI.ForegroundColor = "Green"}
        2 {$host.ui.RawUI.BackgroundColor = "Gray"; $host.ui.RawUI.ForegroundColor = "DarkBlue"}
        3 {$host.ui.RawUI.BackgroundColor = "DarkCyan"; $host.ui.RawUI.ForegroundColor = "DarkYellow"}
        4 {$host.ui.RawUI.BackgroundColor = "DarkGray"; $host.ui.RawUI.ForegroundColor = "DarkRed"}
    }
}

#Calling the user interface (ui) randomizer (then clearing the screen):

random-ui

cls

Prepare thyself.  Are thy prepared?

Here are the next few PowerShell windows I opened:

image image image

Sweet!  Now when I’m working in Active Directory on the BLUE screen, and I want to come back to it, I know which of my sessions to switch back to.

If you end up getting a few screens of the same color and you want a new look, just call the function from the PowerShell session itself:

random-ui

For my function, I’ve left the CLS out of the function itself, so that it doesn’t trash any of the screen that I actually still want to use.

If you prefer, you could put the CLS at the end of the random-ui function (just after the switch is ended) and the screen will refresh after the user interface changes are applied.

Enjoy the new organization of your PowerShell windows!

Twitter: @Michael_Simmons

CategoriesUncategorized