Return Multidimensional Array From Function

PowerShell unrolls arrays returned from a function. Prepend the returned array with the comma operator (,, unary array construction operator) to wrap it in another array, which is unrolled on return, leaving the nested array intact.

function testArray {
    $array1 = @()
    $array1 += ,@("Apple","Banana")

    return ,$array1
}

Leave a Comment