title: Only recurse subfolder x numbers of levels using PowerShell
body: <p>The PowerShell script below will list out all shared folders (excluding hidden shared folders), then list out all sub-folders and finally get the ACL information of each of them and export to a CSV file.</p>
  
  <p>However, I'm trying to set the limit of the sub-folder it can drill into. For example, if I set it to 3, the script will get the ACL information of first three sub-folders. How can I do this?</p>
  
  <p>Input:</p>
  
  <pre><code>path=\\server\sharefolder0\subfolder01\subfolder02
  path=\\server\sharefolder1\subfolder11\subfolder12\subfolder13\subfolder14
  path=\\server\sharefolder2
  </code></pre>
  
  <p>Expected result:</p>
  
  <pre><code>path=\\server\sharefolder0
  path=\\server\sharefolder0\subfolder01
  path=\\server\sharefolder0\subfolder01\subfolder02
  path=\\server\sharefolder1
  path=\\server\sharefolder1\subfolder11
  path=\\server\sharefolder1\subfolder11\subfolder12
  path=\\server\sharefolder2
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>$getSRVlist = Get-Content .\server.txt
  $outputDirPath=".\DirPathList.txt"
  $outputACLInfo=".\ACLInfo.CSV"
  $header="FolderPath,IdentityReference,Rights"
  Del $outputACLInfo
  add-content -value $header -path $outputACLInfo
  foreach ($readSRVlist in $getSRVlist)
  {
     foreach ($readShareInfoList in $getShareInfoList=Get-WmiObject Win32_Share
     -computerName $readSRVlist | Where {$_.name -notlike "*$"} | %{$_.Name})
     {
        foreach ($readDirPathList in
        $getDirPathList=get-childitem \\$readSRVlist\$readShareInfoList -recurse
        | where {$_.PSIsContainer})# | %{$_.fullname})
        {
           $getACLList=get-ACL $readDirPathList.fullname | ForEach-Object
           {$_.Access}
           foreach ($readACLList in $getACLList)
           {
              $a = $readDirPathList.fullname + "," +
              $readACLList.IdentityReference + "," +  $readACLList.FileSystemRights
              add-content -value $a -path $outputACLInfo
           }
        }
     }
  }
  </code></pre>