Search notes:

PowerShell: array subexpression operator

The array subexpression operator $(…) creates an array from the expression within its parentheses. Such an array is created even if the expression evauluates to zero or one element only.

Simple demonstration

In order to demonstrate the array subexpression operator, three functions are created. func_0 returns nothing, func_1 returns a scalar («one element») and func_n returns more than one element (here: 3)
function func_0 {                     }
function func_1 { 'one element '      }
function func_n { 'foo', 'bar', 'baz' }
Github repository about-PowerShell, path: /language/operator/array-subexpression/funcs.ps1
The following snippet embeds these functions into the array subexpression operator and creates three arrays whose number of elements correspond to the number of elements that are returned by the embedded function:
$ary_0 = @( func_0 )
$ary_1 = @( func_1 )
$ary_n = @( func_n )

$ary_0.GetType().FullName  # System.Object[]
$ary_1.GetType().FullName  # System.Object[]
$ary_n.GetType().FullName  # System.Object[]

$ary_0.length              # 0
$ary_1.length              # 1
$ary_n.length              # 3
Github repository about-PowerShell, path: /language/operator/array-subexpression/array-from-funcs.ps1

See also

Compare with @{ … } which creates hash tables.
operator

Index