What is the use of the Get-Error cmdlet in PowerShell?

Get-Error cmdlet was introduced in PowerShell v7. It displays the most recent error messages from the
current session.
When you check the get member of this command, its output is in the form of PSExtendedError so whatever the output is produced by this command is in a detailed manner and so this command is very helpful in troubleshooting error messages.
PS C:\> Get-Error | gm
TypeName: System.Management.Automation.ErrorRecord#PSExtendedError
We will write one command in the PowerShell console which is ultimately generate an error.
PS C:\> Get-ChildItem c:\notexist
Get-ChildItem: Cannot find path 'C:\notexist' because it does not exist.
The above directory does not exist. Let’s get a detailed view of this error using the Get-Error cmdlet.
PS C:\> Get-Error
Exception :
Type : System.Management.Automation.ItemNotFoundException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot find path 'C:\notexist' because it does not exist.
HResult : -2146233087
TargetObject : C:\notexist
CategoryInfo : ObjectNotFound: (C:\notexist:String) [],
ParentContainsErrorRecordException
FullyQualifiedErrorId : PathNotFound
ItemName : C:\notexist
SessionStateCategory : Drive
TargetSite :
Name : GetChildItems
DeclaringType : System.Management.Automation.SessionStateInternal,
System.Management.Automation, Version=7.0.3.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
StackTrace :
at System.Management.Automation.SessionStateInternal.GetChildItems(String path, Boolean
recurse, UInt32 depth, CmdletProviderContext context)
at Microsoft.PowerShell.Commands.GetChildItemCommand.ProcessRecord()
Message : Cannot find path 'C:\notexist' because it does not exist.
Source : System.Management.Automation
HResult : -2146233087
TargetObject : C:\notexist
CategoryInfo : ObjectNotFound: (C:\notexist:String) [Get-ChildItem],
ItemNotFoundException
FullyQualifiedErrorId :
PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
InvocationInfo :
MyCommand : Get-ChildItem
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 94
Line : Get-ChildItem c:\notexist
PositionMessage : At line:1 char:1
+ Get-ChildItem c:\notexist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-ChildItem
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
As you can see in the above output that it contains the error messages, exception details, Invocation Name, Line number for the error, etc. You can access each section separately as well.
PS C:\> (Get-Error).InvocationInfo
MyCommand : Get-ChildItem
BoundParameters : {}
UnboundArguments : {}
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 94
ScriptName :
Line : Get-ChildItem c:\notexist
PositionMessage : At line:1 char:1
+ Get-ChildItem c:\notexist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot :
PSCommandPath :
InvocationName : Get-ChildItem
PipelineLength : 0
PipelinePosition : 0
ExpectingInput : False
CommandOrigin : Internal
DisplayScriptPosition :
PS C:\> (Get-Error).InvocationInfo.PositionMessage
At line:1 char:1
+ Get-ChildItem c:\notexist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
You can also use Get-Error as the pipeline input and for that, you need to error input as the pipeline.The best way is to pass the $Error variable as the input object. It doesn’t matter in which view (Normal, Category, or Concise) the errors are, Get-Error command converts it to the precise view.
$error | Get-Error

You can also get the number of latest errors of the current console using -Newest parameter. For
example,
Get-Error -Newest 2
Whenever we use the above command, you can see the errors are separated by Error Index. In the above example, we have retrieved two latest errors so there will be two error indexes. ErrorIndex 0 and ErrorIndex 1.