title: Accessing elements in an array
body: <p>I have issue with looping through arrays that I am trying to understand.</p>
  
  <p>I have an Array variable that stores the paths of some directories.</p>
  
  <pre><code>$collectorDirArray1 = @(Get-ChildItem -Path D:\APPS\Server* -Filter Data -Recurse -Directory).Fullname
  </code></pre>
  
  <p><code>$collectorDirArray1</code> returns this:</p>
  
  <pre>
  D:\Apps\Server
  D:\Apps\Server1
  D:\Apps\Server2
  </pre>
  
  <p>This <code>foreach</code> loops through the <code>$collectorDirArray1</code> and gets all *.dat files that are 3 minutes or older.</p>
  
  <pre><code>foreach ($file in $collectorDirArray1) {
      $oldDats = @(Get-ChildItem -Path $collectorDirArray1 -Recurse -Filter "*.dat") | Where {$_.LastWriteTime -lt (get-date).AddMinutes(-3)}
  }
  </code></pre>
  
  <p><code>$oldDats</code> returns this:</p>
  
  <pre>
      Directory: D:\APPS\Server\data
  
  Mode                LastWriteTime     Length Name
  
  -a---        2017-10-23   2:40 PM         18 test1.dat
  
      Directory: D:\APPS\Server2\data
  
  
  Mode                LastWriteTime     Length Name
  
  -a---        2017-10-17   4:22 PM         17 test2.dat
  </pre>
  
  <p>This <code>foreach</code> loops to get the <code>LastWriteTime</code> of each file in <code>$oldDats</code> array and sends the output to <code>Write-Host</code>.</p>
  
  <pre><code>foreach ($element in $oldDats) {
      $FileDate = $element.LastWriteTime
      Write-Host "The files that are 3 minutes old are: $oldDats the LastWriteTime is: $FileDate"
  }
  </code></pre>
  
  <p>This returns:</p>
  
  <pre>
  The files that are 3 minutes old are: test1.dat test2.dat the LastWriteTime is: 10/23/2017 14:40:21
  The files that are 3 minutes old are: test1.dat test2.dat the LastWriteTime is: 10/17/2017 16:22:03
  </pre>
  
  <p>I was expecting</p>
  
  <pre>
  The files that are 3 minutes old are: test1.dat the Last Write time is: 10/23/2017 14:40:21
  The files that are 3 minutes old are: test2.dat the Last Write time is: 10/17/2017 16:22:03
  </pre>
  
  <p>If I directly access each element in the array (<code>$oldDats[0]</code> and <code>$oldDats[1]</code>) it returns each file name individually.</p>
  
  <p>Why does the variable <code>$oldDats</code> hold both file names (test1.dat and test2.dat) instead of just the one (test1.dat)?</p>