# Capture all errors to a file

**URL:** <https://forums.powershell.org/t/capture-all-errors-to-a-file/13266>\
**Category:** PowerShell Help\
**Created:** [October 9, 2019, 1:47pm UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266 "2019-10-09T13:47:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)\
**Post date:** [October 9, 2019, 1:47pm UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266/1 "2019-10-09T13:47:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ilikecheese](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@ilikecheese](https://forums.powershell.org/u/ilikecheese)\
**Post date:** [October 10, 2019, 3:40am UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266/2 "2019-10-10T03:40:59Z")

</div>

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.

example

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

---

<div class="post-metadata">

**Author:** ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)\
**Post date:** [November 12, 2019, 3:01pm UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266/3 "2019-11-12T15:01:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2019, 9:50pm UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266/4 "2019-11-12T21:50:09Z")

</div>

You already got it, $error.Invocationinfo.Line

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

---

<div class="post-metadata">

**Author:** ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)\
**Post date:** [November 15, 2019, 3:20pm UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266/5 "2019-11-15T15:20:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/capture-all-errors-to-a-file/13266/6 "2024-05-16T20:31:05Z")

</div>

Closed on May 16, 2024, 8:31pm UTC


