{"id":12621,"date":"2011-09-22T00:01:00","date_gmt":"2011-09-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/09\/22\/format-multilevel-arrays-in-powershell\/"},"modified":"2011-09-22T00:01:00","modified_gmt":"2011-09-22T00:01:00","slug":"format-multilevel-arrays-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/format-multilevel-arrays-in-powershell\/","title":{"rendered":"Format Multilevel Arrays in PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy Ed Wilson talks about formatting multilevel arrays in Windows PowerShell.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I am having a problem with arrays. I have two-level arrays and they work perfectly when I have the arrays defined on a single line. But when I try to format my script so that it is easier to read, they seem to get messed up. Can you help me?&nbsp;<\/p>\n<p>&mdash;DF<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello DF,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Knowing how to work with arrays is fundamental to nearly all programing languages&mdash;at least the ones I know about. In Windows PowerShell, we have removed much of the mystery surrounding arrays and basically hidden the complexity. It is common to use <b>@()<\/b> to create an array, as shown here:<\/p>\n<p style=\"padding-left: 30px\">$b = @(1,2,3)<\/p>\n<p>But using <b>@()<\/b> is not required. For example, I can easily create an array by assigning more than one item to a variable. This technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">$a = 1,2,3<\/p>\n<p>To display the contents of a variable that contains an array, call that variable. To access a specific element in an array, use square brackets to reference the element. These techniques are shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a<\/p>\n<p style=\"padding-left: 30px\">1<\/p>\n<p style=\"padding-left: 30px\">2<\/p>\n<p style=\"padding-left: 30px\">3<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a[0]<\/p>\n<p style=\"padding-left: 30px\">1<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a[2]<\/p>\n<p style=\"padding-left: 30px\">3<\/p>\n<p>If I want to add an extra element to an array, I use the <b>+=<\/b> operator (think of it as adding an item, I want the variable containing the array, and then I make the variable containing the array equal to the new value). The following code illustrates this technique:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a += 6<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $a<\/p>\n<p style=\"padding-left: 30px\">1<\/p>\n<p style=\"padding-left: 30px\">2<\/p>\n<p style=\"padding-left: 30px\">3<\/p>\n<p style=\"padding-left: 30px\">6<\/p>\n<p>All of these techniques are illustrated in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8311.hsg-9-22-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of techniques illustrated\" alt=\"Image of techniques illustrated\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8311.hsg-9-22-11-01.png\" \/><\/a><\/p>\n<p>I can easily create an array that contains an additional array inside one of the elements. For instance, if I want to store the array that is contained in the variable <b>$a<\/b> along with another array in a variable <b>$b<\/b>, I can use the following technique:<\/p>\n<p style=\"padding-left: 30px\">$a = 1,2,3<\/p>\n<p style=\"padding-left: 30px\">$b = $a,@(11,12,13)<\/p>\n<p>If I look at what is stored in <b>$b<\/b>, it is not at first obvious that it comprises two different arrays. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $b<\/p>\n<p style=\"padding-left: 30px\">1<\/p>\n<p style=\"padding-left: 30px\">2<\/p>\n<p style=\"padding-left: 30px\">3<\/p>\n<p style=\"padding-left: 30px\">11<\/p>\n<p style=\"padding-left: 30px\">12<\/p>\n<p style=\"padding-left: 30px\">13<\/p>\n<p>However, I can use square bracket notation and view the array stored in element 1 of the <b>$b<\/b> variable:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $b[1]<\/p>\n<p style=\"padding-left: 30px\">11<\/p>\n<p style=\"padding-left: 30px\">12<\/p>\n<p style=\"padding-left: 30px\">13<\/p>\n<p>I can also access each element of the array:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $b[1][0]<\/p>\n<p style=\"padding-left: 30px\">11<\/p>\n<p>Storing arrays inside arrays in Windows PowerShell is both powerful and very easy to do. When writing a script, however, formatting this can become a problem. The following is one long command to store multiple arrays in the various elements of an array.<\/p>\n<p style=\"padding-left: 30px\">$a = @(0,1,2,3),@(10,11,12,13),@(20,21,22,23),@(30,31,32,33),@(40,41,42,43),@(50,51,52,53)<\/p>\n<p>As shown in the following figure, the multiple dimensions of the array are accessed via square brackets.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2262.hsg-9-22-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of multiple dimensions of array accessed via square brackets\" alt=\"Image of multiple dimensions of array accessed via square brackets\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2262.hsg-9-22-11-02.png\" \/><\/a><\/p>\n<p>DF, in the code you sent to me, you attempt to create another array around the various elements of arrays you have. But when the code runs, it does not work. Here is the code you sent:<\/p>\n<p style=\"padding-left: 30px\">$a = @(@(0,1,2,3)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; ,@(10,11,12,13)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; ,@(20,21,22,23)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; ,@(30,31,32,33)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; ,@(40,41,42,43)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; ,@(50,51,52,53))<\/p>\n<p>When the code runs and I attempt to index into the various elements of the array, the results are munged.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7028.hsg-9-22-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of results munged when attempting to index into array elements\" alt=\"Image of results munged when attempting to index into array elements\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7028.hsg-9-22-11-03.png\" \/><\/a>&nbsp;<\/p>\n<p>It is not necessary to surround the array with another <b>@()<\/b>. The change is rather simple. Just move the commas to the right, and it will work. This is shown in the revised code here:<\/p>\n<p style=\"padding-left: 30px\">$a = @(0,1,2,3),<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; @(10,11,12,13),<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; @(20,21,22,23),<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; @(30,31,32,33),<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; @(40,41,42,43),<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; @(50,51,52,53)<\/p>\n<p>When I run the code, I am able to index into the arrays as shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4503.hsg-9-22-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of successfully indexing into arrays\" alt=\"Image of successfully indexing into arrays\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4503.hsg-9-22-11-04.png\" \/><\/a><\/p>\n<p>When dealing with the necessity of attempting to store multiple arrays into a single array, one pretty good approach is to store those arrays into variables, and then use the variables to build up the new array. This approach is shown here<\/p>\n<p style=\"padding-left: 30px\">$b = 0,1,2,3<\/p>\n<p style=\"padding-left: 30px\">$c = 10,11,12,13<\/p>\n<p style=\"padding-left: 30px\">$d = 20,21,22,23<\/p>\n<p style=\"padding-left: 30px\">$e = 30,31,32,33<\/p>\n<p style=\"padding-left: 30px\">$f = 40,41,42,43<\/p>\n<p style=\"padding-left: 30px\">$g = 50,51,52,53<\/p>\n<p style=\"padding-left: 30px\">$a =&nbsp; $b,$c,$d,$e,$f,$g<\/p>\n<p>When the script runs, the output shown in the following figure is displayed.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4743.hsg-9-22-11-05.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of output displayed when script is run\" alt=\"Image of output displayed when script is run\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4743.hsg-9-22-11-05.png\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Well, DF, that is about all there is to working with and formatting multilevel arrays.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about formatting multilevel arrays in Windows PowerShell. &nbsp; Hey, Scripting Guy! I am having a problem with arrays. I have two-level arrays and they work perfectly when I have the arrays defined on a single line. But when I try to format my script so that it is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[18,3,4,45],"class_list":["post-12621","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about formatting multilevel arrays in Windows PowerShell. &nbsp; Hey, Scripting Guy! I am having a problem with arrays. I have two-level arrays and they work perfectly when I have the arrays defined on a single line. But when I try to format my script so that it is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12621"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12621\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}