Skip to main content

With the PowerShell ErrorVariable common parameter, you can specify a variable that stores errors from the command during processing.

#
# To store the error message in the variable named 'ErrVar' (note: do no prefix the variable with '$');
Get-ChildItem -Path 'path/that/does/not/exist' -ErrorAction SilentlyContinue -ErrorVariable ErrVar
$ErrVar # Prints the errors

#
# To concatenate (potentially) multiple errors, add a '+' to the beginning of the
# error variable after declaring the first command without the '+'; and any additional
# commands thereafter:
Get-ChildItem -Path 'path/that/does/not/exist' -ErrorAction SilentlyContinue -ErrorVariable ErrVar
Get-ChildItem -Path '/some/other/path/that/does/not/exist' -ErrorAction SilentlyContinue -ErrorVariable +ErrVar
$ErrVar # Prints the errors
$ErrVar.GetType() # Gets the data type information (ArrayList)
$ErrVar[0] # Error from first command
$ErrVar[1] # Error from second command