title: Searching a powershell array containing active directory users
body: <p>I'm having an issue trying to find the index number of an item in an array that contains Active directory users.<br>
  I create the array as follows:</p>
  
  <pre><code>$outarray = @()  
  $outarray = get-aduser -Filter * -Properties LastLogon | select "Name","SAMAccountName","LastLogon" | sort samaccountname
  </code></pre>
  
  <p>Now i have the users in an array, and i can prove it using standard variable queries</p>
  
  <pre><code>$outarray[0]  
  $outarray[1]  
  </code></pre>
  
  <p>Returns exactly what i expect.</p>
  
  <p>BUT</p>
  
  <p>I completely fail to search for the index of a <code>name</code> or <code>SAMAccountName</code> in the array, as they are properties of the array.</p>
  
  <pre><code>$index = [array]::IndexOf($outarray.samaccountname, "testuser")  
  </code></pre>
  
  <p>returns -1 (not found) or 0 only if <code>testuser</code> is the FIRST user in the array.
  I cannot find any other user index in the array.</p>
  
  <p>My goal after getting the index is to use it to update the property for lastlogon.  This works if i do it manually
  e.g.</p>
  
  <pre><code>$outarray[123].lastlogon = 12345678
  </code></pre>
  
  <p>The only way i can make this work is to manually build the array initially, one entry at a time instead of filling directly</p>
  
  <pre><code>foreach ($user in $outArray) 
      {
          $myobj = @()
          $myobj = "" | Select "Name","SAMAccountName","LastLogon"
  
          #fill the object
          $myobj.Name = $user.name
          $myobj.SAMAccountName = $user.samaccountname 
          $myobj.LastLogon = $user.LastLogon
  
          #Add the object to the array
          $userarray += $myobj
      }
  $userarray[[array]::IndexOf($userarray.samaccountname, "testuser")].LastLogon = 12345678
  </code></pre>
  
  <p>Then the search works.  I assume this has to do with property types, but im completely out my depth by this stage.  </p>
  
  <p>Thanks in advance for any help, I'm no expert on powershell arrays, they confuse me! :)</p>