{"id":8711,"date":"2007-01-23T17:00:30","date_gmt":"2007-01-23T17:00:30","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/01\/23\/array-literals-in-powershell\/"},"modified":"2019-02-18T13:20:50","modified_gmt":"2019-02-18T20:20:50","slug":"array-literals-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/array-literals-in-powershell\/","title":{"rendered":"Array Literals In PowerShell"},"content":{"rendered":"<p><span style=\"color:#1f497d\">The first thing to understand is that there are no array literals in PowerShell <span style=\"font-family:Wingdings\">J<\/span> Arrays are built using operators or casts. The way to build an array in PowerShell is to use the comma operator as shown in the following examples:\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">$a = , 1\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0# array of one element\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">$a = 1,2 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# array of two elements\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">$a = 1,2,3 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# array of three elements.\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">$a = (1,2),(3,4) \u00a0# array of two elements, each of which is a two element array\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">$a += ,(5,6)\u00a0\u00a0\u00a0\u00a0\u00a0 # add another element which is an array of two element.\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">Now let&#8217;s display the array we&#8217;ve built. Simply displaying the variable isn&#8217;t very helpful since PowerShell will unravel everything in the display process obscuring the inner structure. Instead we&#8217;ll use individual indexing operations to look at each piece:\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (61) &gt; $a[0]\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">1\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">2\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (62) &gt; $a[1]\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">3\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">4\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (63) &gt; $a[2]\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">5\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">6\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">The comma operator is the array construction operator in PowerShell (similar to the cons function in LISP.)\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">By now, if you&#8217;re familiar with PowerShell, you&#8217;re asking \u2013 so what&#8217;s with the @() notation?  If it isn&#8217;t an array literal, then what is it? First let&#8217;s talk about arrays and  casts. The other way to build an array is through a covariant conversions or casts. In PowerShell, this includes the ability to cast a scalar object into an array. In the following example, we&#8217;re casting an integer into an array:\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (64) &gt; $a = [array] 1\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (65) &gt; $a[0]\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">1\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">Now, since it&#8217;s already an array, casting it again doesn&#8217;t result in a second level of nesting:\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (66) &gt; $a = [array] [array] 1\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (67) &gt; $a[0]\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">1\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">However using 2 commas *<strong>does<\/strong>* nest the array since it is the array construction operation:\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (68) &gt; $a = ,,1\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (69) &gt; $a[0][0]\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">1\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">Now on to @(). In essence, the @( \u2026 ) operation is syntactic sugar for\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">[array] $( \u2026 )\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">\u00a0So \u2013 if the statements in @() return a scalar, it will be wrapped in an array but if the result is already an array, then it won&#8217;t be nested.\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">And finally, why did we do it this way?  The goal was to make array operations consistent with the command argument syntax. A comma on the command line indicates a collection of objects bound to one parameter.\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (77) &gt; function f([array] $x, [array] $y, [array] $z) {$x.count; $y.count, $z.count}\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">PS (78) &gt; foo a,b,c z l,m\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">3\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">1\n<\/span><\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">2\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">And why have @() at all? Because pipelines in a value context gather their results from a stream. From the stream you can&#8217;t tell whether the pipeline returned a singleton or a collection. Using the @() means that you don&#8217;t really have to care. If you might receive a collection, then just use\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d;font-family:Courier New;font-size:10pt\">$result = @( \u2026 pipeline \u2026 )\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">and it doesn&#8217;t matter what you receive, it will always be an array.\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">-bruce\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">Bruce Payette [MSFT]\n<\/span><\/p>\n<p><span style=\"color:#1f497d\">Windows PowerShell Tech Lead\n<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p><span style=\"color:#1f497d\">Visit the Windows PowerShell Team blog at:    <a href=\"http:\/\/blogs.msdn.com\/PowerShell\" \/><\/span>http:\/\/blogs.msdn.com\/PowerShell<span style=\"color:#1f497d\">\n\t\t<\/span><\/p>\n<p><span style=\"color:#1f497d\">Visit the Windows PowerShell ScriptCenter at:  <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" \/><\/span>http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<span style=\"color:#1f497d\">\n\t\t<\/span><\/p>\n<p><span style=\"color:#1f497d\">Windows PowerShell in Action (book):  <a href=\"http:\/\/manning.com\/powershell\" \/><\/span>http:\/\/manning.com\/powershell<span style=\"color:#1f497d\">\n\t\t<\/span><\/p>\n<p><span style=\"color:#1f497d\">\n\t\t<\/span>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first thing to understand is that there are no array literals in PowerShell J Arrays are built using operators or casts. The way to build an array in PowerShell is to use the comma operator as shown in the following examples: \u00a0 $a = , 1\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0# array of one element $a = [&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-8711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>The first thing to understand is that there are no array literals in PowerShell J Arrays are built using operators or casts. The way to build an array in PowerShell is to use the comma operator as shown in the following examples: \u00a0 $a = , 1\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0# array of one element $a = [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8711","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=8711"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8711\/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=8711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=8711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=8711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}