Search notes:

PowerShell: common parameter -errorVariable

Invoking a cmdLet with the common parameter -errorVariable causes an error message (if the cmdlet fails) to be stored in the specified variable.
As is also the case with the automatic variable $error, the type of the indicated variable is System.Collections.ArrayList whose elements store System.Management.Automation.ErrorRecord objects.
$item = get-item         `
   inexisting-item       `
  -errorVariable    err  `
  -errorAction      silentlyContinue


if ($err) {
   write-host $err
}
else {
   write-host "item $item found"
}
Github repository about-powershell, path: /language/cmdlet/parameter/common/errorVariable/get-inexisting-item.ps1

See also

The $error automatic variable.
Other common cmdlet parameters.

Index