title: How to find the index of an element in an array by its value using Powershell
body: <p>I have an array of custom objects in Powershell. Each object has a unique reference with which it can be looked up in the array and a value I wish to use later which is not necessarily unique. The array is populated in a for loop as below</p>
  
  <pre><code>#Create empty array
  $array1 = @()
  
  #Populate array
  foreach($otherElement in $otherArray){
    #Create custom array element
    $arrayElement = New-Object PSObject
  
    #Update Credential element   
    $arrayElement | Add-Member -type NoteProperty -Name 'objRef' -Value $($otherElement.getAttribute("ref") )
    $arrayElement | Add-Member -type NoteProperty -Name 'objValue' -Value $($otherElement.getAttribute("someValue") )
  
    #Add element to array
    $array1 += $arrayElement
  }
  </code></pre>
  
  <p>After building the array I want to get access to the <em>objValue</em> corresponding to the correct <em>objRef</em> in some way. I know that you can test if an array contains a value using the <em>-contains</em> parameter but I don't know how to get the index of the object with that value. </p>
  
  <p>Basically this array is acting like a look up table. I want a function to put in an objRef to get out an objValue. </p>
  
  <p>The objValue in this case is a <em>System.Management.Automation.PSCredential</em>, the password for each object is input at runtime for security. At work I have to sometimes install the same software 30 odd times on different machines with the same 5 credentials and figuring this out will help me automate the process reomtely.</p>
  
  <p>Thanks in advance!</p>