views:

932

answers:

2

I find the standard Powershell display of errors (red text, multi-line display) a bit distracting. Is it possible to customize this?

+5  A: 

Yes and yes.

You can use the built-in $host object if all you want to do is change the text color. However, you can't change the error message itself - that's hardcoded.

What you could do is (a) suppress the error messages, and instead (b) trap the errors and display your own.

Accomplish (a) by setting $ErrorActionPreference = "SilentlyContinue" - this won't STOP the error, but it suppresses the messages.

Accomplishing (b) requires a bit more work. By default, most PowerShell commands don't produce a trappable exception. So you'll have to learn to run commands and add the -EA "Stop" parameter to generate a trappable exception if something goes wrong. Once you've done that, you can create a trap in the shell by typing:

trap { # handle the error here }

You could put this in your profile script rather than typing it every time. Inside the trap, you can output whatever error text you like by using the Write-Error cmdlet.

Probably more work than you were wanting to do, but that's basically how you'd do what you asked.

Don Jones
+3  A: 

Here is a bunch of stuff that will let you customize your console output. You can set these settings as you like in your profile, or make functions/scripts to change them for different purposes. Maybe you want a "Don't bug me" mode sometimes, or a "Show me everything going wrong" at others. You could make a function/script to change between those.

## Change colors of regular text
$Host.UI.RawUI.BackGroundColor = "DarkMagenta"
$Host.UI.RawUI.ForeGroundColor = "DarkYellow" 

## Change colors of special messages (defaults shown)
$Host.PrivateData.DebugBackgroundColor = "Black"
$Host.PrivateData.DebugForegroundColor = "Yellow"
$Host.PrivateData.ErrorBackgroundColor = "Black"
$Host.PrivateData.ErrorForegroundColor = "Red"
$Host.PrivateData.ProgressBackgroundColor = "DarkCyan"
$Host.PrivateData.ProgressForegroundColor = "Yellow"
$Host.PrivateData.VerboseBackgroundColor = "Black"
$Host.PrivateData.VerboseForegroundColor = "Yellow"
$Host.PrivateData.WarningBackgroundColor = "Black"
$Host.PrivateData.WarningForegroundColor = "Yellow"

## Set the format for displaying Exceptions (default shown)
## Set this to "CategoryView" to get less verbose, more structured output
## http://blogs.msdn.com/powershell/archive/2006/06/21/641010.aspx
$ErrorView = "NormalView"

## More control over display of Exceptions (defaults shown), if you want more output
$ReportErrorShowExceptionClass = 0
$ReportErrorShowInnerException = 0
$ReportErrorShowSource = 1
$ReportErrorShowStackTrace = 0

## Set display of special messages (defaults shown)
## http://blogs.msdn.com/powershell/archive/2006/07/04/Use-of-Preference-Variables-to-control-behavior-of-streams.aspx
## http://blogs.msdn.com/powershell/archive/2006/12/15/confirmpreference.aspx
$ConfirmPreference = "High"
$DebugPreference = "SilentlyContinue"
$ErrorActionPreference = "Continue"
$ProgressPreference = "Continue"
$VerbosePreference = "SilentlyContinue"
$WarningPreference = "Continue"
$WhatIfPreference = 0

You can also use the -ErrorAction and -ErrorVariable parameters on cmdlets to affect only that cmdlet call. The second one will send errors to the specified variable instead of the default $Error.

JasonMArcher