#PSTip How to clear the $error variable
Note: This tip requires PowerShell 2.0 or above.
PowerShell has the $error automatic variable. It contains a collection of the errors that occurred while the PowerShell engine has been running. The collection in $error is an instance of System.Collections.ArrayList. The most recent error is the first error object in a collection–$Error[0]. The number of errors that are retained is controlled by the $MaximumErrorCount preference variable (set to 256 by default). You can increase that number up to 32768, but that would increase the memory usage as well. Default value is usually big enough.
What to do if you want to clean out all the entries in $error? $error is a variable, so you can try with the Clear-Variable cmdlet:
PS> Clear-Variable error -Force Clear-Variable : Cannot overwrite variable Error because it is read-only or constant.
Unfortunately, that doesn’t work even when you use the -Force parameter.
$error is also an object, so maybe the Get-Member cmdlet will reveal a useful method:
PS> $error | Get-Member
TypeName: System.Management.Automation.ErrorRecord
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System....
GetType Method type GetType()
ToString Method string ToString()
writeErrorStream NoteProperty System.Boolean writeErrorStream=True
CategoryInfo Property System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;}
ErrorDetails Property System.Management.Automation.ErrorDetails ErrorDetails {get;set;}
Exception Property System.Exception Exception {get;}
FullyQualifiedErrorId Property string FullyQualifiedErrorId {get;}
InvocationInfo Property System.Management.Automation.InvocationInfo InvocationInfo {get;}
PipelineIterationInfo Property System.Collections.ObjectModel.ReadOnlyCollection[int] PipelineIterationInfo {g...
ScriptStackTrace Property string ScriptStackTrace {get;}
TargetObject Property System.Object TargetObject {get;}
PSMessageDetails ScriptProperty System.Object PSMessageDetails {get=& { Set-StrictMode -Version 1; $this.Except...
This approach doesn’t work either, because you are getting information about the error objects (System.Management.Automation.ErrorRecord type) contained in the $error variable, not the members of the $error itself. Do you remember our previous tip #PSTip Getting information about a collection object, not its elements? Yes, you need to use -InputObject parameter. You can easily spot the Clear() method now:
PS> Get-Member -InputObject $error
TypeName: System.Collections.ArrayList
Name MemberType Definition
---- ---------- ----------
Add Method int Add(System.Object value), int IList.Add(System.Object value)
AddRange Method void AddRange(System.Collections.ICollection c)
BinarySearch Method int BinarySearch(int index, int count, System.Object value, System.Collections....
Clear Method void Clear(), void IList.Clear()
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
Contains Method bool Contains(System.Object item), bool IList.Contains(System.Object value)
...
...
You could clean out all the entries in $error by calling $error.Clear().
Share on: