{"id":4431,"date":"2009-02-27T16:25:00","date_gmt":"2009-02-27T16:25:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/02\/27\/converting-to-array\/"},"modified":"2019-02-18T13:12:48","modified_gmt":"2019-02-18T20:12:48","slug":"converting-to-array","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/converting-to-array\/","title":{"rendered":"Converting to Array"},"content":{"rendered":"<p>When you run a PowerShell pipeline, that pipeline might return&nbsp;0, 1 or many items.&nbsp; If you are assigning the results of that pipeline to a variable, you will get $null, 1 item or an array of items respectively.&nbsp; Sometimes, you won&#8217;t care about the different types returned, but at other times, you&#8217;ll actually prefer to force the results to always be an array.&nbsp; In those cases, you can use @( &#8230; ).&nbsp; As in:<\/p>\n<p>$a = @(get-childitem)<\/p>\n<p>&nbsp;That will evaluate the statements enclosed within the parentheses and collect the results into an array.&nbsp; If there are no results, then you&#8217;ll get an array of length zero.&nbsp; <\/p>\n<p>This usually works well enough, but I found that I usually decide that I want an array after I&#8217;m well into writing&nbsp;the pipeline.&nbsp; I would then have to move the cursor back to the beginning of the pipeline, to insert the &#8220;@(&#8220;.&nbsp; After repeating that enough times I got annoyed and decided I&#8217;d rather just append something at the end of the pipeline that would convert the results to an array.&nbsp; Thus was born my ToArray function.<\/p>\n<p>function ToArray<br \/>{<br \/>&nbsp; begin<br \/>&nbsp; {<br \/>&nbsp;&nbsp;&nbsp; $output = @(); <br \/>&nbsp; }<br \/>&nbsp; process<br \/>&nbsp; {<br \/>&nbsp;&nbsp;&nbsp; $output += $_; <br \/>&nbsp; }<br \/>&nbsp; end<br \/>&nbsp; {<br \/>&nbsp;&nbsp;&nbsp; return ,$output; <br \/>&nbsp; }<br \/>}<\/p>\n<p>Simple enough right?&nbsp; The begin block creates a new empty array.&nbsp; The process block, which gets called for every pipeline item, adds that item to the array, and the end block just puts the array into an array of length 1 and writes that array to the pipeline.&nbsp; I need to wrap the $output array in a 1 element array because the pipeline will unravel all&nbsp;enumerables (well, almost all).&nbsp; This way it just unravels that 1 element array and what&#8217;s left is my original array.<\/p>\n<p>One thing you should know is that this isn&#8217;t exactly the most performant way to do this.&nbsp;&nbsp;For every item I&#8217;m&nbsp;creating a brand new array.&nbsp;&nbsp;If I was going to pipe a lot of items I&nbsp;would probably be better off using an ArrayList and converting it to an array at the end.&nbsp; I&#8217;ll&nbsp;leave that part as an exercise for you.<\/p>\n<p>This works for me.&nbsp; It&#8217;s simple and it makes my life easier.&nbsp; Now whenever I need to make the results into an array I just pipe into ToArray.&nbsp; No more having to to go back to the beginning of the pipeline for me. \ud83d\ude42<\/p>\n<p>$a = get-childitem | ToArray<\/p>\n<p><span>&#8211; Marcel Ortiz Soto [MSFT]<\/span><\/p>\n<p>P.S. if ToArray is too long for you, create an alias.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you run a PowerShell pipeline, that pipeline might return&nbsp;0, 1 or many items.&nbsp; If you are assigning the results of that pipeline to a variable, you will get $null, 1 item or an array of items respectively.&nbsp; Sometimes, you won&#8217;t care about the different types returned, but at other times, you&#8217;ll actually prefer to [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>When you run a PowerShell pipeline, that pipeline might return&nbsp;0, 1 or many items.&nbsp; If you are assigning the results of that pipeline to a variable, you will get $null, 1 item or an array of items respectively.&nbsp; Sometimes, you won&#8217;t care about the different types returned, but at other times, you&#8217;ll actually prefer to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4431"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}