Note: The problem was first discovered with -OutVariable - see #3154.
The problem applies to all -*Variable common parameters, however, with the exception of -PipelineVariable (which inherently captures a single item at time).
To recap the problem:
-
Even if only a single value is captured by these parameters, an array list rather than a scalar is returned (the single-element collection isn't unwrapped - a deviation from PowerShell's normal collection handling).
-
Also - another deviation from usual PowerShell behavior - it is type [System.Collections.ArrayList] that is always used, in contrast with the [System.Object[]] instances you get with regular assignments.
Steps to reproduce
# Adv. function that outputs a single item of each type.
function foo {
[CmdletBinding()]
param()
Write-Warning 'warn'
Write-Information 'info'
Write-Error 'err'
'out'
}
foo -OutVariable ov -ErrorVariable ev -WarningVariable wv -InformationVariable iv *>$null
$ov, $ev, $wv, $iv | % GetType | % Name
Expected behavior
String
ErrorRecord
WarningRecord
InformationRecord
Actual behavior
ArrayList
ArrayList
ArrayList
ArrayList
Environment data
PowerShell Core v6.0.0-beta (v6.0.0-beta.1)