Return Arrays

by Sep 2, 2011

Normally, PowerShell will not preserve the type of an array returned by a function. It is always reduced to Object[]:

function test { 
  $al=[System.Collections.ArrayList](1..10)
  $al
}
(Test).GetType().FullName

By adding a comma (telling PowerShell that it should return the entire array), you will see that things change:

function test { 
  $al=[System.Collections.ArrayList](1..10)
  ,$al
}
(Test).GetType().FullName

Twitter This Tip!
ReTweet this Tip!