I want to output many different foreground colors with one statement.
PS C:\\> Write-Host \"Red\" -ForegroundColor Red
Red
This output i
I found a much easier option at https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/17/writing-output-with-powershell/
Basically, the first write-host includes the option -NoNewLine. This prevents the new line from forming. The next write-host will be added immediately after the previous text. And each of the separate write-host can have -foregroundcolor options. That can be repeated for each color change you need.
Example with one line of text with three colors:
write-host "Your text here " -ForeGroundColor Red -NoNewLine
write-host "some other text here " -ForeGroundColor Yellow -NoNewLine
write-host "And the last text here."
Notice that there is a space after the text in the first and second write-host. PowerShell is not concatenating or combining the text, it is simply not moving the cursor to the next line.
I was trying to run this on a Windows Server 2012R2 box under ISE and the function by Jesse Chisholm was failing because for some reason (Get-Host).UI.RawUII.ForegroundColor was -1. To stop this happening and to simplify the function I little I modified it as follows:
function Write-ColorText
{
# DO NOT SPECIFY param(...)
# we parse colors ourselves.
$allColors = ("-Black", "-DarkBlue","-DarkGreen","-DarkCyan","-DarkRed","-DarkMagenta","-DarkYellow","-Gray",
"-Darkgray","-Blue", "-Green", "-Cyan", "-Red", "-Magenta", "-Yellow", "-White",
"-Foreground")
$color = "Foreground"
$nonewline = $false
foreach($arg in $args)
{
if ($arg -eq "-nonewline")
{
$nonewline = $true
}
elseif ($allColors -contains $arg)
{
$color = $arg.substring(1)
}
else
{
if ($color -eq "Foreground")
{
Write-Host $arg -nonewline
}
else
{
Write-Host $arg -foreground $color -nonewline
}
}
}
Write-Host -nonewline:$nonewline
}
I know this is an old post but hopefully this is useful to somebody and thanks Jesse for giving me this wonderful function!!
Here is small a function I wrote to output colored text (it is actually smaller, but I rewrote it to be more understandable):
function Write-Color() {
Param (
[string] $text = $(Write-Error "You must specify some text"),
[switch] $NoNewLine = $false
)
$startColor = $host.UI.RawUI.ForegroundColor;
$text.Split( [char]"{", [char]"}" ) | ForEach-Object { $i = 0; } {
if ($i % 2 -eq 0) {
Write-Host $_ -NoNewline;
} else {
if ($_ -in [enum]::GetNames("ConsoleColor")) {
$host.UI.RawUI.ForegroundColor = ($_ -as [System.ConsoleColor]);
}
}
$i++;
}
if (!$NoNewLine) {
Write-Host;
}
$host.UI.RawUI.ForegroundColor = $startColor;
}
It's quite simple to use: just use Write-Color "your text"
and add some color name between curly brackets where you want the text to be colored.
Examples:
`Write-Color "Hello, {red}my dear {green}friend !"` will output
Script screenshot
You can put it in your $profile
file to use it in a simple PowerShell prompt, or just add it to some scripts.
This works too...
Write-Host "Don't forget to " -ForegroundColor Yellow -NoNewline; Write-Host "CALL YOUR MOM " -ForegroundColor Red -NoNewline; Write-Host "every day!" -ForegroundColor Yellow
Here is a simplistic way to do this
if ($help)
{
Write-Host " For the switch " -NoNewline; Write-Host " -userUniqueId" -ForegroundColor Green -NoNewline; Write-Host ", enter an email address or samaccountname (pin) so '-userUniqueId 123456' "
Write-Host "";
Write-Host " For the switch " -NoNewline; Write-Host " -disableMFAForUser" -ForegroundColor Green -NoNewline; Write-Host ", enter an email address or samaccountname (pin) with the -userUniqueId and then '-disableMFAForUser $true' "
Write-Host "";
Write-Host " For the switch " -NoNewline; Write-Host "-enableMFAForUser" -ForegroundColor Green -NoNewline; Write-Host ", enter an email address or samaccountname (pin) with the -userUniqueId and then '-enableMFAForUser $true' "
Write-Host "";
Write-Host " For the switch " -NoNewline; Write-Host "-verifyAllMFAEnabled" -ForegroundColor Green -NoNewline; Write-Host ", enter '-verifyAllMFAEnabled $true' "
Write-Host "";
Write-Host " For the switch " -NoNewline; Write-Host " -verifyAllMFADisabledSpecificUser" -ForegroundColor Green -NoNewline; Write-Host ", enter an email address or samaccountname (pin) and then '-verifyAllMFADisabledSpecificUser $true' "
Write-Host "";
return;
}
This function provides different syntactic sugar:
function color-Write
{
# DO NOT SPECIFY param(...)
# we parse colors ourselves.
$allColors = ("-Black", "-DarkBlue","-DarkGreen","-DarkCyan","-DarkRed","-DarkMagenta","-DarkYellow","-Gray",
"-Darkgray","-Blue", "-Green", "-Cyan", "-Red", "-Magenta", "-Yellow", "-White")
$foreground = (Get-Host).UI.RawUI.ForegroundColor # current foreground
$color = $foreground
[bool]$nonewline = $false
$sofar = ""
$total = ""
foreach($arg in $args)
{
if ($arg -eq "-nonewline") { $nonewline = $true }
elseif ($arg -eq "-foreground")
{
if ($sofar) { Write-Host $sofar -foreground $color -nonewline }
$color = $foregrnd
$sofar = ""
}
elseif ($allColors -contains $arg)
{
if ($sofar) { Write-Host $sofar -foreground $color -nonewline }
$color = $arg.substring(1)
$sofar = ""
}
else
{
$sofar += "$arg "
$total += "$arg "
}
}
# last bit done special
if (!$nonewline)
{
Write-Host $sofar -foreground $color
}
elseif($sofar)
{
Write-Host $sofar -foreground $color -nonewline
}
}
Examples:
color-Write This is normal text
color-Write Normal -Red Red -White White -Blue Blue -ForeGround Normal