title: Path does not exists in powershell foreach script
body: <p>I'm having a strange situation, and I can't figure out the problem. I want the get the ACL and the AccessRules for all subdirectories of a given path. If I do them individually, I don't get any errors</p>
  
  <pre><code>gci -recurse | Where-Object { $_.PSIsContainer } | Get-Acl | Format-List | Out-File C:\temp\permission.txt
  gci -recurse | Where-Object { $_.PSIsContainer } | Get-Acl | foreach {$_.GetAccessRules($true, $true, [System.Security.Principal.NTAccount])} | Out-File C:\temp\permission1.txt -Append
  </code></pre>
  
  <p>However, I want to execute this in a foreach loop, to manipulate and handle better how the file is generated. Here's what I'm trying to do in my script:</p>
  
  <pre><code>sl c:\test_folder
  gci * -Recurse | Export-Csv c:\temp\dir.csv -Force
  $pastas = gci -Recurse | where {$_.PsIsContainer}
  if (Test-Path C:\temp\permission.txt)
  {
    ri c:\temp\permission.txt
  }
  foreach ($pasta in $pastas)
  {
    $pasta
    Test-Path $pasta
    $acl = get-acl $pasta
    $acl | format-list | Out-File -FilePath c:\temp\permission1.txt -Append
    $acl.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) | Out-File -FilePath c:\temp\permission1.txt -Append
  }
  </code></pre>
  
  <p>While I'm processing in the <code>foreach</code> the folders directly in the root of the <code>$pastas</code> variable, the <code>Test-Path</code> returns <code>true</code>. In the first sub-directory however, the <code>Test-Path</code> returns <code>false</code>, but the folder does exist. On the get-acl, I get PathNotFound exception:</p>
  
  <pre><code>Get-Acl : Não é possível localizar o caminho 'Exportacao' porque ele não existe.
  Em C:\Temp\Script Get Info.ps1:12 caractere:17
  +   $acl = get-acl &lt;&lt;&lt;&lt;  $pasta
      + CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
      + FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
  </code></pre>
  
  <p>What am I doing wrong?</p>