{"id":2833,"date":"2013-09-23T00:01:00","date_gmt":"2013-09-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/09\/23\/adding-array-index-numbers-to-powershell-custom-objects\/"},"modified":"2013-09-23T00:01:00","modified_gmt":"2013-09-23T00:01:00","slug":"adding-array-index-numbers-to-powershell-custom-objects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/adding-array-index-numbers-to-powershell-custom-objects\/","title":{"rendered":"Adding Array Index Numbers to PowerShell Custom Objects"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about adding array index numbers to Windows PowerShell custom objects.<\/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;Hey, Scripting Guy! Our software application returns arrays of information. What I need is a way to artificially add the array element number to the object that the cmdlet outputs. For example, I want to add the highlighted information to the following output, and I need the highlighted numbers to be the array indexes:<\/p>\n<p style=\"padding-left: 30px\"><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0184.01.PNG\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0184.01.PNG\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Can you help me?<\/p>\n<p>&mdash;WS<\/p>\n<p><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;Hello WS,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well it is just a couple of days until the Scripting Wife&rsquo;s birthday. Shhhhhh&hellip;don&rsquo;t tell her that I have not forgotten. It is sort of a game we like to play. I pretend to forget it is her birthday, she pretends to be upset. In the end, I wind up getting her great presents that she really likes&mdash;such as the complete series of the old Get Smart television show. She is still talking about that present!<\/p>\n<p>So WS, when I first read your email sent to <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, I thought, &ldquo;Well, that will not be too hard. Maybe I will use <strong>Select-Object<\/strong>, add a custom column, and pick up the index number.&rdquo;<\/p>\n<p>Well unfortunately, that did not work out too well.<\/p>\n<p>I do not have your custom software package installed, but that really does not matter because PowerShell is PowerShell is PowerShell. It always behaves the same.<\/p>\n<p>What I ended up doing was creating a custom PSObject and using the <strong>IndexOf<\/strong> method to pick up the index number. Then I added in the value of the array element. Here is an example of what I am talking about:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array = &#8220;dog&#8221;,&#8221;cat&#8221;,&#8221;mongoose&#8221;,&#8221;bird&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array.IndexOf(&#8216;dog&#8217;)<\/p>\n<p style=\"padding-left: 30px\">0<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $array.IndexOf(&#8216;mongoose&#8217;)<\/p>\n<p style=\"padding-left: 30px\">2<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<p>As you can see in the previous script, the <strong>IndexOf<\/strong> method will accept an array value and return the associated index number from it. So here is how I added this into a little demo script:<\/p>\n<p style=\"padding-left: 30px\"># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\"># Script: ArrayIndexAndValues.ps1<\/p>\n<p style=\"padding-left: 30px\"># Author: ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\"># Date: 09\/06\/2013 11:33:26<\/p>\n<p style=\"padding-left: 30px\"># Keywords: Scripting Techniques, Arrays, Custom PSObjects<\/p>\n<p style=\"padding-left: 30px\"># comments:<\/p>\n<p style=\"padding-left: 30px\"># HSG-9-23-2013<\/p>\n<p style=\"padding-left: 30px\"># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">$array = &#8220;dog&#8221;,&#8221;cat&#8221;,&#8221;mongoose&#8221;,&#8221;bird&#8221;<\/p>\n<p style=\"padding-left: 30px\">Foreach($a in $array)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">New-Object -TypeName psobject -Property @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; index= $array.indexof($A)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Value= $A } }<\/p>\n<p>But I was not really happy with my answer. So I emailed Lee Holmes and asked his opinion. Here is his answer&hellip;<\/p>\n<p>First of all, there is no really nice way to do what you are attempting to do. There&rsquo;s an example from my Windows PowerShell Cookbook, second version, which talks about how to add <strong><a href=\"http:\/\/poshcode.org\/2129\" target=\"_blank\">Add-FormatTableIndexParameter<\/a><\/strong> to <strong>Format-Table<\/strong>. But other than that, an approach similar to yours is good.<\/p>\n<p>Using <strong>Array.IndexOf()<\/strong> will be a little slow. Here is an alternative:<\/p>\n<p style=\"padding-left: 30px\">dir c:\\ | Foreach-Object { $index = 0 } {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [PSCustomObject] @{ Index = $index; Object = $_ }; $index++<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>WS, that is all there is to using Windows PowerShell to add array index values to a custom object. Join me tomorrow when we will have a great guest blog post by Microsoft PowerShell MVP, Jeffrey Hicks. He&rsquo;ll be talking about a script profile tool that he wrote. It is good stuff and you do not want to miss out.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about adding array index numbers to Windows PowerShell custom objects. &nbsp;Hey, Scripting Guy! Our software application returns arrays of information. What I need is a way to artificially add the array element number to the object that the cmdlet outputs. For example, I want to add the highlighted [&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":[442,3,4,45],"class_list":["post-2833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about adding array index numbers to Windows PowerShell custom objects. &nbsp;Hey, Scripting Guy! Our software application returns arrays of information. What I need is a way to artificially add the array element number to the object that the cmdlet outputs. For example, I want to add the highlighted [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2833","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=2833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2833\/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=2833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}