liveBook · Manning

ErrorVariable

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

ErrorVariable is a PowerShell common parameter that directs command errors to a named variable for later access.

ErrorVariable is a PowerShell common parameter that sends command errors to a named variable so you can inspect or log them later. Unlike the automatic $Error variable, it requires you to choose the destination name, without a leading $. section 24.0

Collecting connection errors

$computer = 'Srv01','DC01','Web02' $session = New-PSSession -ComputerName $computer -ErrorVariable a The variable a receives errors from the command. By default, ErrorVariable keeps only the most recent error. Prefix the name with + to append errors to existing data: -ErrorVariable +a. section 24.0 section 24.4

Use ErrorVariable when continuing execution is acceptable but failures must remain visible:

  • Combine it with an appropriate ErrorAction rather than suppressing useful errors. section 24.4
  • Use the collected errors for logging or later inspection. section 24.0
  • Pair it with Try/Catch when handling terminating exceptions. section 24.4
New-PsSession -ComputerName SRV01 -ErrorVariable a

Compare single-value collection with the append form shown in b2. section 24.0

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains named storage, most-recent behavior, appending, ErrorAction, and Try/Catch, then applies them to remoting.
Examples
Uses New-PSSession against multiple computers and a lab function that retrieves remote uptime.
What it assumes
Assumes basic PowerShell commands and variables while reinforcing ErrorAction, Try/Catch, remoting, and automatic variables.
What you can do after
Diagnose command failures by capturing, appending, inspecting, and logging errors from remoting and other commands.
How it connects
Connects ErrorVariable with common parameters, ErrorAction, $Error, Try/Catch, exceptions, remoting, and logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.