PowerShell output on screen

It’s always nice to have some screen output while running scripts interactively, in this blog post I will show you a few ways to do that.

Write-Error

Using the Write-Errror cmdlet, you can show an error message in the screen output in red. For example:

Write-Error -Message ("Error processing {0}" -f $server.name)

will show you the following:

Write-Host

When using the Write-Host cmdlet, you can use the -ForegrouncColor parameter to specify a color. Personally, I always use Green for regular logging so that other messages (errors/warnings) are easier to spot because their colors are different. For example:

Write-Host ("Processing {0}" -f $server.name) -ForegroundColor Green

will show you the following:

Write-Output

This will show you the output in a basic format. It can, however in comparison to Write-Host, send the text as an object for the next pipeline. For example:

Write-Output ("Error processing {0}" -f $server.name

will show you the following:

Write-Verbose

You can use Write-Verbose messages in your scripts to only show output when running the script if the -Verbose parameter was specified, more like a debug option. For example, running this script:

[CmdletBinding()]
Param(
)
$items = Get-childitem d:\scripts
$numberofitems = 0
foreach ($item in $items) {
    write-verbose ("Processing {0}" -f $item.fullname)
    $numberofitems++
}
Write-Host ("Amount of files: {0}" -f $numberofitems) -ForegroundColor Green

When running the script without the -Verbose parameter, the output will be:

But when running the script with the -Verbose parameter, the output will be like this and show you the files too:

Write-Warning

You can display warning messages that pop out more than normal Write-Host messages. For example:

Write-Warning ("Error processing {0}" -f $server.name)

will show you the following:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.