Is it possible to customize error display in powershell?

Paul Moore picture Paul Moore · Dec 15, 2008 · Viewed 13.4k times · Source

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

Answer

Don Jones picture Don Jones · Dec 16, 2008

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.