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
