{"id":11861,"date":"2011-12-07T00:01:00","date_gmt":"2011-12-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/12\/07\/find-the-index-number-of-a-value-in-a-powershell-array\/"},"modified":"2011-12-07T00:01:00","modified_gmt":"2011-12-07T00:01:00","slug":"find-the-index-number-of-a-value-in-a-powershell-array","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/find-the-index-number-of-a-value-in-a-powershell-array\/","title":{"rendered":"Find the Index Number of a Value in a PowerShell Array"},"content":{"rendered":"<p><b>Summary<\/b>: In this blog post, Microsoft Scripting Guy, Ed Wilson, talks about finding the index number of a value in a Windows PowerShell array.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The other day, the Scripting Wife and I were invited to speak with a customer in Charlotte. Our contact, Rafael, is a really cool guy (he is also a SQL Server MVP). We met Rafael at various <a href=\"http:\/\/www.sqlsaturday.com\/\" target=\"_blank\">SQL Saturday<\/a> events in the southern portion of the United States. It was great to see our friend again, and the group was extremely engaged and asked lots of great questions. One question I received had to do with arrays, namely, &ldquo;How can I determine the element number of an array to see the data in that array?&rdquo;<\/p>\n<p><b>Note<\/b>: This is the third blog post in a series devoted to working with arrays in Windows PowerShell. In the first post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/12\/05\/learn-simple-ways-to-handle-windows-powershell-arrays.aspx\" target=\"_blank\">Learn Simple Ways to Handle Windows PowerShell Arrays<\/a><i>,<\/i> I discussed creating arrays, indexing into arrays, and two techniques for walking through an array. In yesterday&rsquo;s post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/12\/06\/add-modify-verify-and-sort-your-powershell-array.aspx\" target=\"_blank\">Add, Modify, Verify, and Sort Your PowerShell Array<\/a>,<i> <\/i>I discussed adding, modifying, or verifying values in a Windows PowerShell array, and two easy techniques for sorting the array.<\/p>\n<h2>Find the index of value in an array<\/h2>\n<p>The first thing I need to do is create an array that contains a number of random numbers. (This is not a requirement to find an index value in an array, but it is a requirement for the demo. Also, being able to create a random array like this is useful when it comes to practicing with arrays.) I like to use the <b>Get-Random <\/b>cmdlet to do this because it is easier than doing a bunch of typing. Here is the command to create an array that contains 10 random numbers.<\/p>\n<p style=\"padding-left: 30px\">$array = Get-Random -Count 10 -in(1..100)<\/p>\n<h2>Use the for<i> <\/i>statement<\/h2>\n<p>Now, I use a <b>for<\/b><i> <\/i>statement with a pipeline that begins at zero, a test pipeline that continues until <b>i<\/b> is 1 less than the <b>length<\/b><i> <\/i>of the array, and an increment pipeline that increases the value of <b>i<\/b> by 1. Here is the for condition.<\/p>\n<p style=\"padding-left: 30px\">for($i=0;$i-le $array.length-1;$i++)<\/p>\n<p>The script block that is associated with the <b>for<\/b><i> <\/i>statement uses parameter substitution and the format operator to display the counter (<b>$i<\/b>) value and the value that is contained in the specific array element. Here is the script block portion of the <b>for<\/b><i> <\/i>statement.<\/p>\n<p style=\"padding-left: 30px\">{&#8220;`$array[{0}] = {1}&#8221; -f $i,$array[$i]}<\/p>\n<p>The command to create an array of random numbers and display the array element numbers in addition to the values that are contained in the array element are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2287.hsg-12-7-11-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2287.hsg-12-7-11-01.png\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<h2>Using the indexof<i> <\/i>static method<\/h2>\n<p>The <b>System.Array <\/b>.NET Framework class contains a number of static methods that are useful. These static methods are shown here (they are all well documented on MSDN).<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [array] | gm -s | select name, definition | ft -a<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">AsReadOnly&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Collections.ObjectModel.ReadOnlyCollection[T] AsRea&#8230;<\/p>\n<p style=\"padding-left: 30px\">BinarySearch&nbsp;&nbsp;&nbsp; static int BinarySearch(array array, System.Object value), static&#8230;<\/p>\n<p style=\"padding-left: 30px\">Clear&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Clear(array array, int index, int length)<\/p>\n<p style=\"padding-left: 30px\">ConstrainedCopy static System.Void ConstrainedCopy(array sourceArray, int sourceI&#8230;<\/p>\n<p style=\"padding-left: 30px\">ConvertAll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static TOutput[] ConvertAll[TInput, TOutput](TInput[] array, Syst&#8230;<\/p>\n<p style=\"padding-left: 30px\">Copy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Copy(array sourceArray, array destinationArray&#8230;<\/p>\n<p style=\"padding-left: 30px\">CreateInstance&nbsp; static array CreateInstance(type elementType, int length), static&#8230;<\/p>\n<p style=\"padding-left: 30px\">Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static bool Equals(System.Object objA, System.Object objB)<\/p>\n<p style=\"padding-left: 30px\">Exists&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static bool Exists[T](T[] array, System.Predicate[T] match)<\/p>\n<p style=\"padding-left: 30px\">Find&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static T Find[T](T[] array, System.Predicate[T] match)<\/p>\n<p style=\"padding-left: 30px\">FindAll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static T[] FindAll[T](T[] array, System.Predicate[T] match)<\/p>\n<p style=\"padding-left: 30px\">FindIndex&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static int FindIndex[T](T[] array, System.Predicate[T] match), st&#8230;<\/p>\n<p style=\"padding-left: 30px\">FindLast&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static T FindLast[T](T[] array, System.Predicate[T] match)<\/p>\n<p style=\"padding-left: 30px\">FindLastIndex&nbsp;&nbsp; static int FindLastIndex[T](T[] array, System.Predicate[T] match)&#8230;<\/p>\n<p style=\"padding-left: 30px\">ForEach&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Void ForEach[T](T[] array, System.Action[T] action)<\/p>\n<p style=\"padding-left: 30px\">IndexOf&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static int IndexOf(array array, System.Object value), static int &#8230;<\/p>\n<p style=\"padding-left: 30px\">LastIndexOf&nbsp;&nbsp;&nbsp;&nbsp; static int LastIndexOf(array array, System.Object value), static &#8230;<\/p>\n<p style=\"padding-left: 30px\">ReferenceEquals static bool ReferenceEquals(System.Object objA, System.Object objB)<\/p>\n<p style=\"padding-left: 30px\">Resize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Resize[T](T[]&amp; array, int newSize)<\/p>\n<p style=\"padding-left: 30px\">Reverse&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Reverse(array array), static System.Void Rever&#8230;<\/p>\n<p style=\"padding-left: 30px\">Sort&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Sort(array array), static System.Void Sort(arr&#8230;<\/p>\n<p style=\"padding-left: 30px\">TrueForAll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static bool TrueForAll[T](T[] array, System.Predicate[T] match)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>To use the <b>indexof<\/b><i> <\/i>static method, I provide an array and a value that I want to find. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">[array]::indexof($array,39)<\/p>\n<p>The command to create an array of 10 random numbers, display the contents of the array, find the index number of one item in the array, and then verify that value is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6116.hsg-12-7-11-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6116.hsg-12-7-11-02.png\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<h2>Work with one half of the array<\/h2>\n<p>It is common to need to work with one half of an array at a time. As it turns out, this is incredibly easy to do&mdash;I simply use the range operator. To test this, I first want to create an array with 20 elements in it. To do this, I use the range operator and store the array of numbers in a variable. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$array = 1..20<\/p>\n<p>Next, I test to ensure that I have 20 elements in my array. I do this by displaying the content as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array<\/p>\n<p style=\"padding-left: 30px\">1<\/p>\n<p style=\"padding-left: 30px\">2<\/p>\n<p style=\"padding-left: 30px\">&lt;output truncated&gt;<\/p>\n<p style=\"padding-left: 30px\">20<\/p>\n<p>Instead of displaying the content of a rather large array, I could use the <b>Count<\/b><i> <\/i>or the <b>Length<\/b><i> <\/i>properties as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array.count<\/p>\n<p style=\"padding-left: 30px\">20<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array.Length<\/p>\n<p style=\"padding-left: 30px\">20<b><\/b><\/p>\n<p>I now use the range operator and the <b>GetUpperBound<\/b><i> <\/i>method to display the first half of the array. This command and its associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array[0..($array.GetUpperBound(0)\/2)]<\/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\">4<\/p>\n<p style=\"padding-left: 30px\">5<\/p>\n<p style=\"padding-left: 30px\">6<\/p>\n<p style=\"padding-left: 30px\">7<\/p>\n<p style=\"padding-left: 30px\">8<\/p>\n<p style=\"padding-left: 30px\">9<\/p>\n<p style=\"padding-left: 30px\">10<\/p>\n<p style=\"padding-left: 30px\">11<\/p>\n<p>Well, the previous command works alright, but the problem seems to be that it displayed one additional element: &ndash;element 11. To fix this requires subtracting one from the upper boundary of the array. Here is the revised code and its output.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array[0..(($array.GetUpperBound(0)\/2)-1)]<\/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\">4<\/p>\n<p style=\"padding-left: 30px\">5<\/p>\n<p style=\"padding-left: 30px\">6<\/p>\n<p style=\"padding-left: 30px\">7<\/p>\n<p style=\"padding-left: 30px\">8<\/p>\n<p style=\"padding-left: 30px\">9<\/p>\n<p>Bummer! That did not work as I had expected. I then decide to examine the value with which I am working. Here is the result.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ($array.GetUpperBound(0)\/2)<\/p>\n<p style=\"padding-left: 30px\">9.5<\/p>\n<p>How did I get 9.5 when I have 20 elements in my array? Well, this is because the upper boundary of the array is 19 (remember we start counting at 0). I then decide to &ldquo;cheat&rdquo; and convert the value to an integer. The revised command and output are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array[0..([int]($array.GetUpperBound(0)\/2)-1)]<\/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\">4<\/p>\n<p style=\"padding-left: 30px\">5<\/p>\n<p style=\"padding-left: 30px\">6<\/p>\n<p style=\"padding-left: 30px\">7<\/p>\n<p style=\"padding-left: 30px\">8<\/p>\n<p style=\"padding-left: 30px\">9<\/p>\n<p style=\"padding-left: 30px\">10<\/p>\n<p>With this in mind, I can now access the second half of the array. Here is my command to accomplish that task.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array[[int]($array.GetUpperBound(0)\/2)..($array.GetUpperBound(0))]<\/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 style=\"padding-left: 30px\">14<\/p>\n<p style=\"padding-left: 30px\">15<\/p>\n<p style=\"padding-left: 30px\">16<\/p>\n<p style=\"padding-left: 30px\">17<\/p>\n<p style=\"padding-left: 30px\">18<\/p>\n<p style=\"padding-left: 30px\">19<\/p>\n<p style=\"padding-left: 30px\">20<\/p>\n<p>That is all there is to finding a value in an array when you do not know the index number. Array Week will continue tomorrow when I will talk about working with arrays of 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\" target=\"_blank\">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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In this blog post, Microsoft Scripting Guy, Ed Wilson, talks about finding the index number of a value in a Windows PowerShell array. Microsoft Scripting Guy, Ed Wilson, is here. The other day, the Scripting Wife and I were invited to speak with a customer in Charlotte. Our contact, Rafael, is a really cool [&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":[292,51,3,4,45],"class_list":["post-11861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hashtables-and-dictionary-objects","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: In this blog post, Microsoft Scripting Guy, Ed Wilson, talks about finding the index number of a value in a Windows PowerShell array. Microsoft Scripting Guy, Ed Wilson, is here. The other day, the Scripting Wife and I were invited to speak with a customer in Charlotte. Our contact, Rafael, is a really cool [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11861","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=11861"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11861\/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=11861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}