16

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

4 Answers 4

14

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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"

## NOTE: This section is only for PowerShell 1.0, it is not used in PowerShell 2.0 and later
## 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.

4 Comments

Note that the $ReportErrorShow* variables do not actually have any effect in PowerShell 2.0. See technet.microsoft.com/en-us/library/dd347675.aspx
Good point, PS v2.0 has a different system now. I will update my answer.
This is fantastic.
Sadly, MS broke those blog links during one of their many blog platform migrations, I'm not currently up for proof reading the new versions, and the wayback machine URLs are too long to use in code blocks. However, I did find these URLs for the first article: new home and wayback machine
3

This may or may not be what you want, but there is a $ErrorView preference variable that you can set:

$ErrorView = "CategoryView"

This gives a shorter one-line error message, for example:

[PS]> get-item D:\blah
ObjectNotFound: (D:\blah:String) [Get-Item], ItemNotFoundException

Comments

2

Also, you can do this to write a specific line of error text:

$Host.UI.WriteErrorLine("This is an error")

(props to Chris Sears for this answer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.