header image

Error view

Posted by: | October 29, 2019 Comments Off on Error view |

Error view is another experimental feature introduced with PowerShell v7 preveiw 5. The experimental feature needs to be enabled and PowerShell restarted.

PS> Enable-ExperimentalFeature -Name PSErrorView
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.

 

At the PowerShell prompt you’d normally see an error in this form

PS> 1 / 0
Attempted to divide by zero.
At line:1 char:1
+ 1 / 0
+ ~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

 

The PSErrorview feature changes this to a more concise one line error

PS> 1 / 0
RuntimeException: Attempted to divide by zero.

 

Enabling the experimental feature sets the $errorview preference variable to ConciseView

PS> $errorview
ConciseView

 

You can set the error view manually

PS> $errorview = [System.Management.Automation.ErrorView]::NormalView
PS> 1/0
Attempted to divide by zero.
At line:1 char:1
+ 1/0
+ ~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

 

Possible values for $errorview are

PS> [enum]::GetValues([System.Management.Automation.ErrorView])
NormalView
CategoryView
ConciseView

 

In Normal view an error in a script looks like this

PS> .\test.ps1
Attempted to divide by zero.
At C:\Scripts\test.ps1:1 char:2
+ 1 / 0
+ ~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

 

In Concise view you get this

PS> $errorview = [System.Management.Automation.ErrorView]::ConciseView
PS> .\test.ps1
RuntimeException: C:\Scripts\test.ps1
Line |
1       | 1 / 0
| ^ Attempted to divide by zero.

 

The concise view supplies a more obvious indication of where the error is occurring.

 

This is a new feature that looks useful especially if you spend a lot of time working interactively.

under: PowerShell 7

Comments are closed.

Categories