# Output $error\[0\] Variable to Readable Format

**URL:** <https://forums.powershell.org/t/output-error-0-variable-to-readable-format/14699>\
**Category:** PowerShell Help\
**Created:** [July 23, 2020, 8:44am UTC](https://forums.powershell.org/t/output-error-0-variable-to-readable-format/14699 "2020-07-23T08:44:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjlieb](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@jjlieb](https://forums.powershell.org/u/jjlieb)\
**Post date:** [July 23, 2020, 8:44am UTC](https://forums.powershell.org/t/output-error-0-variable-to-readable-format/14699/1 "2020-07-23T08:44:53Z")

</div>

Hi All,

I’ve got a script that runs reports against a vCenter server. It will tell me whether the VM exists or not. Everything works great except for the error var. I’m trying to output the $error I have below into a CSV file so that it’s easy to remove the characters before and after the single quotation marks. In the example below ‘testing’ is the VM that I can’t run a report against since it doesn’t exist in vCenter. I’m running this command to generate the output but I’d like it cleaned up a bit.

$error[0] | Group-Object | Format-Table Name -AutoSize | Out-String

Here is the output:

7/23/2020 2:10:42 PM Get-VM VM with name ‘testing’ was not found using the …

The date and time will change in each row that gets populated in the CSV file as the report is generated against the VM’s.

Any ideas would be appreciated!

Thanks,

Jim

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 23, 2020, 10:33am UTC](https://forums.powershell.org/t/output-error-0-variable-to-readable-format/14699/2 "2020-07-23T10:33:35Z")

</div>

Jim, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

When you post code, error messages, sample data or console output format it as code, please.  
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.  
Thanks in advance

There are several problems with the approach. $Error is all errors, not specifically a vm not being found, using the index 0 is just the last error that occurred. Not sure what the Group-Object is doing, but there should just be the last error and nothing to really group. Format-Table should be the end of pipeline. A better approach is to capture the error in a try catch:

```
try {
    $vm = Get-VM -Name $vmName -ErrorAction Stop
}
catch {
    'Error occurred connecting to VM {0}.{1}' -f $vmName, $_
}
```

There is a [error e-book](https://leanpub.com/thebigbookofpowershellerrorhandling) in the Resources section that you may want to take a look at as well

---

<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:29pm UTC](https://forums.powershell.org/t/output-error-0-variable-to-readable-format/14699/3 "2024-05-16T20:29:10Z")

</div>

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


