{"id":4454,"date":"2012-12-18T11:59:00","date_gmt":"2012-12-18T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/12\/18\/powertip-select-the-next-to-last-item-in-a-powershell-array\/"},"modified":"2012-12-18T11:59:00","modified_gmt":"2012-12-18T11:59:00","slug":"powertip-select-the-next-to-last-item-in-a-powershell-array","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-select-the-next-to-last-item-in-a-powershell-array\/","title":{"rendered":"PowerTip: Select the Next-to-Last Item in a PowerShell Array"},"content":{"rendered":"<p><strong>Summary:<\/strong>&nbsp;&nbsp;Learn how to select the next-to-last item in a Windows PowerShell array.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;I have a collection of objects in an array, and I need to select the next-to-last item in the array. How can this be done?<\/p>\n<p style=\"padding-left: 60px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Good question &hellip; Charlotte Windows PowerShell user group member Brian Wilhite says: Every now and then, there are times where I have to do just such a thing. Consider the following example:<\/p>\n<p style=\"padding-left: 90px\">$Objects = @(&lsquo;First&rsquo;,&rsquo;Second&rsquo;,&rsquo;Third&rsquo;,&rsquo;Fourth&rsquo;,&rsquo;Fifth&rsquo;)<\/p>\n<p style=\"padding-left: 60px\">The example below will select the first item in the array.<\/p>\n<p style=\"padding-left: 90px\">$Objects[0]<\/p>\n<p style=\"padding-left: 60px\">If you use [-1] instead, it will select the very last item in the array. As you may have already deduced, you can use the [-2] index value to select the next-to-last item in the array, as the example below demonstrates.<\/p>\n<p style=\"padding-left: 60px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2703.QT-12-18-12.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2703.QT-12-18-12.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"\" border=\"0\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary:&nbsp;&nbsp;Learn how to select the next-to-last item in a Windows PowerShell array. &nbsp;I have a collection of objects in an array, and I need to select the next-to-last item in the array. How can this be done? &nbsp;Good question &hellip; Charlotte Windows PowerShell user group member Brian Wilhite says: Every now and then, there are [&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":[327,56,356,3,45],"class_list":["post-4454","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-brian-wilhite","tag-guest-blogger","tag-powertip","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary:&nbsp;&nbsp;Learn how to select the next-to-last item in a Windows PowerShell array. &nbsp;I have a collection of objects in an array, and I need to select the next-to-last item in the array. How can this be done? &nbsp;Good question &hellip; Charlotte Windows PowerShell user group member Brian Wilhite says: Every now and then, there are [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4454","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=4454"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4454\/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=4454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}