Returning Array in One Chunk

by Sep 5, 2011

In a previous tip, we showed you how adding a single comma can change the way functions return arrays. With this trick, you can force a PowerShell function to return an array in one chunk. Have a look:

function test { (1..10) } 
Test | ForEach-Object { "Receiving $_" }

Arrays returned by functions are by default unwrapped and processed as single values. With a simple comma, this will change:

function test { ,(1..10) } 
Test | ForEach-Object { "Receiving $_" }

Twitter This Tip!
ReTweet this Tip!