Powershell Functions do not return single element arrays

I came across an interesting problem today that a function I wrote to return all SCCM primary sites worked at work but did not work at home. the difference between 2 SCCM environments is that I only have 1 single SCCM site in my home environment comparing to large multi-tier SCCM infrastructure at work. after some investigation I found out this common issue with Powershell when comes to returning arrays from a function.

For example:


The first function foo should returns a single element arraylist, and the variable it returned has the same type as the type of the only element in the arraylist.

The second function boo should returns a two-element arraylist but the type of returned variable has changed from .NET arraylist to normal powershell array.

So how should I get Powershell to return the same variable type in a function when it’s some sort of array?

In the return statement inside of the function, add a comma (“,”) in front of variable:

.NET ArrayList:



Normal Powershell arrays:


More readings about this here and here


  1. What is really missing in this port is what everyone is looking for. How to get more than one thing out of a function. Well I am going to share what everyone wants to know who has searched and found this hoping it will answer the question.

    function My-Function([string]$IfYouWant)
    [hashtable]$Return = @{}

    $Return.Success = $False
    $Return.date = get-date
    $Return.Computer = Get-Host

    Return $Return
    #End Function

    $GetItOut = My-Function
    Write-host “The Process was $($GetItOut.Success) on the date $($GetItOut.date) on the host $($GetItOut.Computer)”

    #You could then do
    $var1 = $GetItOut.Success
    $Var2 =$GetItOut.date
    $Var3 = $GetItOut.Computer

    If ($var1 –like “True”){write-host “Its True, Its True”}

    Peter S

    1. This may be a touch simpler:

      function xxx {
      return $false, (get-date), (get-host)

      $flg, $dt, $h = xxx


  2. Pingback: Label Printing

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: