PowerShell – Choose between colored host text or write to an output file? Not anymore!

5 Aug

In PowerShell multiple Write statements are available. The two statements I want to address are Write-Host and Write-Output. 

What do these two do?
Write-Host: Writes customized output to a host
Write-Output: Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console 

An example of Write-Host

Write-Host "Write-Host statement..."

The result displayed in the host 

 

An example of Write-Output

Write-Output "Write-Output statement..."

The result displayed in the host

 

At this point there is no difference in the result of these two statements. 

Write-Host with ForegroundColor 

A nice parameter of Write-Host is -ForegroundColor. Using this parameter with a color, the text displayed in the host has the color specified. I find this very useful to quickly see if a script has succeeded or failed. I always display the success message in green, the failed message in red.   

To show this the existence of a SPList will be checked. If it doesn´t exist, it will be created using the following statement:   

$sampleListName = "PS List"
$siteUrl=http://sp2010dev
$site = Get-SPSite $siteUrl
$web = $site.RootWeb</pre>

function CreateList($listName){
  $web.Lists.Add($listName, "Description for list", [Microsoft.SharePoint.SPListTemplateType]::GenericList)
}

function CheckIfListExists(){
$currentList = $web.Lists.TryGetList($sampleListName)
  if($currentList -ne $null){
    Write-Host "List exists" -ForegroundColor Green
  }
  else{
    Write-Host "List doesn't exist, creating now..." -ForegroundColor Red
    CreateList $sampleListName
    Write-Host "Check if list exist..."
    CheckIfListExists
  }
}

Write-Host "Calling CheckIfListExists ..."
CheckIfListExists

If the list exists a message with green colored text will appear: ‘List exists’, else a message with red colored text will appear: ‘List doesn’t exist, creating now…’, followed by the actual creation of the list. After this the existence of the list is check again. As you can see if the list creation fails the code will be stuck in an infinite loop, so don´t use this as production code…   

Outcome when the list doesn´t exist and is created:

Outcome when the list already exists:<picture PSWrite02>

Write-Output with output to file   

A nice option of Write-Output is the possibility to write the output to a file on the filesystem.   

$file = "C:\_Tools\OutputFile.txt"
Write-Output "Write-Output statement..."
Write-Output "Write-Output statement..." | Out-File $file

The first Write-Output statement writes the output to the host, the second to the file (but not to the host).   

Best of both worlds?   

Ofcourse Write-Host as well as Write-Output has a lot more options to use, but I want to point out something here.
Write-Host can write colored messages to the host, but it can’t write output to a file.
Write-Output can’t write colored messages to the host, but it can write output to a file.   

What if I want to:   

write colored messages to the host if I decide to output to the host AND
write messages to a file just by using ‘| Out-File $file’

With the current possibilities of both operations I have to choose between:
1. colored messages (and nothing written to the output file) or put the output to a file and host, but no colored messages displayed in the host 
2. write double statements; Write-Host with -ForeGroundColor to write the colored output to the host and Write-Output to write the output to a file when using ‘| Out-File $file’.   

Let me clarify the second option with an example.

Write-Output "Write-Output statement..." | Out-File $file
Write-Host "Write-Host statement..." -ForegroundColor Blue

Write-Output writes the message to the file specified in $file and is not written to the host.
Write-Host writes the message to the host in the color blue.
Just as expected and just want I wanted. But I have to write the messages twice.

Another option:   

Write-Output "Write-Output statement..."
Write-Host "Write-Host statement..." -ForegroundColor Blue

The code above is saved in a file and called by:   


PS C:\_Tools> .\PSScriptFile.ps1

Result: messages appear twice in the host.

Output it to a file:   


PS C:\_Tools> .\PSScriptFile.ps1 | Out-File $file

Result: No message in the host, but a message in the file!
Still, the messages have to be written twice in the file: Write-Host and Write-Output…
Do I really have to write all the messages twice to show them colored in the host and write them to an output file?
Actually, there is no possibility to make Write-Host write to an output file.
But the host itself can be accessed to change its text color (and more) and use Write-Output to write to the host (in color!) and to an output file.

The host itself can be accessed by $Host.   


$Host.Name

When using PowerGUI Script Editor the results of the above statement is: PowerGUIScriptEditorHost
Executing the same statement in the console the result is: ConsoleHost

That’s awesome, $Host knows who he is! 🙂

Let’s try to get the text color of the host:


$currentColor = $Host.UI.RawUI.ForegroundColor

PowerGUI Script Editor:

Console:

So what about changing the text color of the host?   


function WriteCustomOutput($message, [System.ConsoleColor]$foregroundcolor)
{
  currentColor = $Host.UI.RawUI.ForegroundColor
  $Host.UI.RawUI.ForegroundColor = $foregroundcolor
  if ($message)
  {
    Write-Output $message
  }
  $Host.UI.RawUI.ForegroundColor = $currentColor
}

WriteCustomOutput -message "WriteColoredOutput statement..." -foregroundcolor Green

And the result in the host:

   

Let’s save the code to a file and call the file from the console:

 

And will the output be written to an output file when using the statement:


.\PSWriteCustomOutput.ps1 | Out-File OutputFile.txt

Yes it is written to the file:

   

Summary

Write-Host and Write-Output are very simple and useful statements. Sometimes the best of both is wished for.
With some creativity you can have both. Use WriteCustomOutput!

One Reply to “PowerShell – Choose between colored host text or write to an output file? Not anymore!”

Comments are closed.