{"id":5120,"date":"2012-08-15T11:59:00","date_gmt":"2012-08-15T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/15\/powertip-the-finer-points-of-finessing-an-array\/"},"modified":"2012-08-15T11:59:00","modified_gmt":"2012-08-15T11:59:00","slug":"powertip-the-finer-points-of-finessing-an-array","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-the-finer-points-of-finessing-an-array\/","title":{"rendered":"PowerTip: The Finer Points of Finessing an Array"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to replace items in an array and how to sort an 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\" \/>I need to replace the &ldquo;2&rdquo; with &ldquo;12&rdquo; in the <b>$array<\/b>&nbsp;variable&nbsp;shown here:<\/p>\n<p style=\"padding-left: 60px\">$array = &#8220;1&#8221;,&#8221;2&#8243;,&#8221;3&#8243;,&#8221;4&#8243;<\/p>\n<p style=\"padding-left: 30px\">How can I do this?<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/><\/p>\n<ol style=\"padding-left: 30px\">\n<ol>\n<li>$array=[regex]::replace($array,&#8221;2&#8243;,&#8221;12&#8243;)<\/li>\n<li>$array = $array -replace &#8220;2&#8221;,&#8221;12&#8243;<\/li>\n<li>$array.SetValue(&#8220;12&#8221;,1)<\/li>\n<\/ol>\n<\/ol>\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\" \/>I have an array defined in the&nbsp;<b>$array<\/b>&nbsp;variable shown here.<\/p>\n<p style=\"padding-left: 60px\">$array = 2,5,9,12,3,5<\/p>\n<p style=\"padding-left: 30px\">What is the easiest way to sort the array?<\/p>\n<p style=\"padding-left: 30px\"><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;<\/p>\n<p style=\"padding-left: 60px\">1. Sort the array by using the&nbsp;<b>sort<\/b><i>&nbsp;<\/i>static method from the [array] .NET Framework class.<\/p>\n<p style=\"padding-left: 90px\">[array]::sort($array)<\/p>\n<p style=\"padding-left: 60px\">2. Pipe the array to the&nbsp;<b>Sort-Object<\/b>&nbsp;cmdlet, and store the results back in the&nbsp;<b>$array<\/b>&nbsp;variable.<\/p>\n<p style=\"padding-left: 90px\">&nbsp;$array = $array | sort<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2502.7331.Scripto.jpg-150x0.jpg\" style=\"line-height: 0px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2502.7331.Scripto.jpg-150x0.jpg\" border=\"0\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to replace items in an array and how to sort an array. I need to replace the &ldquo;2&rdquo; with &ldquo;12&rdquo; in the $array&nbsp;variable&nbsp;shown here: $array = &#8220;1&#8221;,&#8221;2&#8243;,&#8221;3&#8243;,&#8221;4&#8243; How can I do this? &nbsp; $array=[regex]::replace($array,&#8221;2&#8243;,&#8221;12&#8243;) $array = $array -replace &#8220;2&#8221;,&#8221;12&#8243; $array.SetValue(&#8220;12&#8221;,1) I have an array defined in the&nbsp;$array&nbsp;variable shown here. $array = 2,5,9,12,3,5 [&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":[356,3,45],"class_list":["post-5120","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powertip","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to replace items in an array and how to sort an array. I need to replace the &ldquo;2&rdquo; with &ldquo;12&rdquo; in the $array&nbsp;variable&nbsp;shown here: $array = &#8220;1&#8221;,&#8221;2&#8243;,&#8221;3&#8243;,&#8221;4&#8243; How can I do this? &nbsp; $array=[regex]::replace($array,&#8221;2&#8243;,&#8221;12&#8243;) $array = $array -replace &#8220;2&#8221;,&#8221;12&#8243; $array.SetValue(&#8220;12&#8221;,1) I have an array defined in the&nbsp;$array&nbsp;variable shown here. $array = 2,5,9,12,3,5 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5120","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=5120"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5120\/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=5120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}