tags:

views:

339

answers:

3

Is there a way to clear the $Error variable that tracks errors in a PowerShell session?

If so, how do you do it?

I have tried: $error.clear

In the PowerShell ISE on Windows 7 and the $Error array is still populated.

+1  A: 

Does PowerShell have a command you can run which always succeeds, thus clearing $Error? (true on *nix is one such program, to compare.)

Roger Pate
The problem is that $Error is an array that tracks errors for the entire session. As new errors occur they are added. Nothing is replaced.
Matt Spradley
That's correct except that there is a max size for the number of error records retained which is specified by the preference variable $MaximumErrorCount.
Keith Hill
Oh yeah, technically it is an System.Collections.ArrayList which is where the Clear method comes from.
Keith Hill
+7  A: 

It is a .NET method call so you need parens:

$error.clear()
Keith Hill
A: 
$True

thats usually true. I dont know if you can set it to be false. I guess you might unless it's readonly. Ofc wouldnt recommend setting it to something else. Probably system-independent translations exist like if(1), but I wouldn't guarantee that.

if($True) {write-host "Foo!"}

then you can execute

if($False) {write-host "not"}
jormis
How it this an answer to the above question???
Hinek