Summary: MSH Color Functions
Shown below are two functions that allow you to control the console's colors from scripts or the command line.
As for using the functions... personally, I just dumped them into my profile.msh, but you're welcome to stick them where you please.
Also, I've included a cool example of a custom prompt at the bottom that uses these color functions.
Enjoy.
#
# MSH Color Functions
#
# Author: Daniel Keep
# Email: $("nobots.daniel.keep@gmail.com").substring(7)
# Version: 1.0
# License: BSD
#
# This script contains two functions which can be used
# to easily set the console foreground and background
# colors. The error-handling is rudimentary AT BEST.
#
# A nice addition would be a "color echo" function that
# allows you to use ANSI escape sequences (or something
# comparable).
#
#
# Copyright (c) 2005, Daniel Keep
# All rights reserved.
#
# Redistribution and use in source and binary forms, with
# or without modification, are permitted provided that the
# following conditions are met:
#
# * Redistributions of source code must retain the
# above copyright notice, this list of conditions and
# the following disclaimer.
# * Redistributions in binary form must reproduce the
# above copyright notice, this list of conditions and
# the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# * Neither the name of the copyright holder nor the
# names of any contributors may be used to endorse or
# promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# set-foreground [ bright | dark ] COLOR
#
# Sets the console's foreground color to the specified color.
#
# The "bright" and "dark" arguments specify if the color should
# be intense (bright) or not (dark). If omitted, "dark" is
# assumed.
#
# Valid colors are: black, blue, green, aqua, red, purple,
# yellow and white.
#
function set-foreground
{
$local:color = $Args[0].ToLower()
$local:offset = 0
$local:value = 0
if( $local:color -ieq "bright" )
{
$local:color = $args[1].ToLower()
$local:offset = 8
}
elseif( $local:color -ieq "dark" )
{
$local:color = $args[1].tolower()
$local:offset = 0
}
if( $local:color -eq -1 )
{
echo "Usage: set-foreground [ dark | bright ] COLOR"
echo " Valid colors are: black, blue, green, aqua,"
echo " red, purple, yellow and white."
}
switch( $local:color )
{
black { $local:value = 0 }
blue { $local:value = 1 }
green { $local:value = 2 }
aqua { $local:value = 3 }
red { $local:value = 4 }
purple { $local:value = 5 }
yellow { $local:value = 6 }
white { $local:value = 7 }
}
[$MshHost.ui.rawui.foregroundColor] = $local:value + $local:offset
}
#
# set-background [ bright | dark ] COLOR
#
# Sets the console's background color to the specified color.
#
# The "bright" and "dark" arguments specify if the color should
# be intense (bright) or not (dark). If omitted, "dark" is
# assumed.
#
# Valid colors are: black, blue, green, aqua, red, purple,
# yellow and white.
#
function set-background
{
$local:color = $Args[0].ToLower()
$local:offset = 0
$local:value = 0
if( $local:color -ieq "bright" )
{
$local:color = $args[1].ToLower()
$local:offset = 8
}
elseif( $local:color -ieq "dark" )
{
$local:color = $args[1].tolower()
$local:offset = 0
}
if( $local:color -eq -1 )
{
echo "Usage: set-background [ dark | bright ] COLOR"
echo " Valid colors are: black, blue, green, aqua,"
echo " red, purple, yellow and white."
}
switch( $local:color )
{
black { $local:value = 0 }
blue { $local:value = 1 }
green { $local:value = 2 }
aqua { $local:value = 3 }
red { $local:value = 4 }
purple { $local:value = 5 }
yellow { $local:value = 6 }
white { $local:value = 7 }
}
[$MshHost.ui.rawui.backgroundColor] = $local:value + $local:offset
}
#
# Here is an example of what you can do with this. This replicates
# the cool prompt that comes with the default installation of
# Cygwin (http://www.cygwin.com/) in full color!
#
# To use it, just copy both the above color functions, and the below
# prompt function into your profile.msh (just paste it in at the end),
# and then uncomment the prompt function below.
#
#function prompt
#{
# $user = $env:username
# $host = $(get-hostname).hostname
# $loc = get-location
#
# # Since write-object doesn't allow us to supress the
# # trailing newline (at least, the help doesn't SAY it
# # does), we'll cheat a bit and use write-host for the
# # first line, then write-object for the last.
# #
# # Just FYI, if you don't write an object into the
# # pipeline at some point, MSH seems to output it's
# # default prompt automatically.
#
# set-foreground green
# write-host -n "`n$user@$host "
# set-foreground yellow
# write-host -n "$loc`n"
# set-foreground white
# write-object "`$ "
#}