Capture all errors to a file

Is there a way to capture all the errors to a csv file with complete information? I need it to a file because these are task scheduled scripts.

Any ideas? My main goal is to get the complete error data.

The $Error variable captures all occurring errors. You could just pipe it to export-csv, or select just the properties you want to have in your csv.


$Error | select targetobject,exception | Export-Csv c:\temp\errorfile.csv -NoTypeInformation

In your above example how would I get information like $error.Invocationinfo.Line piece of the data.

I would like to select pieces of the data and one of my most needed pieces is Invocationinfo.Line. To see the command that failed.

You already got it, $error.Invocationinfo.Line

You can use a Foreach-Object on $Error and then pick Invocationinfo.Line in the process scriptblock.

Do I have to collect those into a new object then to export all this into a csv?