Skip to content

Common parameters -OutVariable, -ErrorVariable, -WarningVariable, and -InformationVariable always return a [System.Collections.ArrayList] instance, even with a single object #3773

@mklement0

Description

@mklement0

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifWG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions