What is the use of $ErrorView in PowerShell?


$Errorview variable determines the display format of the error message in PowerShell. Before PowerShell 7 there were mainly two views,

  • Normal View (Default view)

  • Category View

With PowerShell version 7, one new additional error view category is included and now there are 3 $ErrorView categories for version 7.

  • Concise View (Default)

  • Normal View

  • Category View

We will understand each view one by one.

A ) Normal View

It is the default view before PowerShell version 7 and it produces the detailed multiline errors and bit noisy. It includes the exception name, category, line number of the error, etc.

$ErrorView = 'NormalView'
Get-ChildItem C:\NoDirectory

Output

Get-ChildItem : Cannot find path 'C:\NoDirectory' because it does not exist.
At line:1 char:1
+ Get-ChildItem C:\NoDirectory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\NoDirectory:String) [Get-ChildItem],
ItemNotFoundException
+ FullyQualifiedErrorId :
PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

B) Category View

Single liner and structured view, designed for the production environment. Its format is as below.

{Category}: ({TargetName}:{TargetType}):[{Activity}], {Reason}

For example

$ErrorView = 'CategoryView'
Get-ChildItem C:\NoDirectory

Output

ObjectNotFound: (C:\NoDirectory:String) [Get-ChildItem], ItemNotFoundException

C) Concise View

The default view in PowerShell version 7. It provides a concise error message. If the error is from thecommand line it’s a single line error message.

For example

$ErrorView = 'ConciseView'
Get-ChildItem C:\NoDirectory

Output

Get-ChildItem: Cannot find path 'C:\NoDirectory' because it does not exist.

If the error is from the script then it is a multiline error message that contains the error message and the line number for the error.

$ErrorView = 'ConciseView'
PS C:\> C:\Temp\TestPS1.ps1

Output

Error message in Concise view
Get-ChildItem: C:\Temp\TestPS1.ps1:2
Line |
2 | Get-ChildItem c:
onDirectory | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot find path 'C:
onDirectory' because it does not exist.

Updated on: 19-Sep-2020

962 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements