Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to customize error display in powershell?

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

like image 969
Paul Moore Avatar asked Dec 15 '08 22:12

Paul Moore


People also ask

How do you show error messages in PowerShell?

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.

How do you handle errors in PowerShell?

Use the try block to define a section of a script in which you want PowerShell to monitor for errors. When an error occurs within the try block, the error is first saved to the $Error automatic variable. PowerShell then searches for a catch block to handle the error.

How do you throw a custom exception in PowerShell?

To create our own exception event, we throw an exception with the throw keyword. This creates a runtime exception that is a terminating error. It's handled by a catch in a calling function or exits the script with a message like this.

How do I hide an error in PowerShell?

If you need to suppress an error, you can use the ErrorAction switch to suppress an error for a single cmdlet or an ErrorAction preference variable to suppress errors globally.


4 Answers

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.

like image 192
Don Jones Avatar answered Oct 15 '22 15:10

Don Jones


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.

like image 45
JasonMArcher Avatar answered Oct 15 '22 14:10

JasonMArcher


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
like image 35
Charlie Joynt Avatar answered Oct 15 '22 14:10

Charlie Joynt


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)

like image 21
Neil Avatar answered Oct 15 '22 14:10

Neil