title: Limiting recursion depth when searching for folders
body: <p>I am trying to limit the recursion depth for a folder search script that I wrote. I am trying to limit to <strong>up to five levels deep</strong></p>
  
  <p>Essentially I want to get something like this:</p>
  
  <pre><code>h:\demo\1st level
  h:\demo\1st level\2nd level
  h:\demo\1st level\2nd level\3rd level
  h:\demo\1st level\2nd level\3rd level\4th level\
  h:\demo\1st level\2nd level\3rd level\4th level\5th level
  </code></pre>
  
  <p>Here is the code that I have:</p>
  
  <pre><code>function Get-ChildItemRecursive {
      [CmdletBinding()]
      Param(
          [Parameter(Mandatory=$true, ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
  
          [string]$FullName,
          [Parameter(Mandatory=$false)]
  
          [int]$Depth = 0
      )
  
      Process {
          if ($Depth -ne 1) {
              $newDepth = if ($Depth -gt 1) { $Depth - 1 } else { $Depth }
              Get-ChildItem -Path $FullName -Directory |
                  Get-ChildItemRecursive -Depth $newDepth
          }
          else {
              Get-ChildItem -Path $FullName -Directory
          }
          Get-ChildItem -Path $FullName -File
      }
  }
  
  Get-ChildItemRecursive -FullName 'H:\demo\' |
     Where {$_.PSIsContainer -eq $True} | select @{Name='Date Modified';
  Expression = {$_.LastWriteTime.ToString('MM/dd/yyyy')}},
               @{Name='Owner';E=
                   {(($_.GetAccessControl().Owner.Split('\'))[1])}},
               FullName | Export-Csv 'H:\demo\scan1.csv' -NoTypeInformation
  </code></pre>
  
  <p>The output I am getting:</p>
  
  <pre><code>Get-ChildItemRecursive -FullName 'H:\demo\' |
      Where {$_.PSIsContainer -eq $True} | select   @{Name='Date Modified';
  Expression = {$_.LastWriteTime.ToString('MM/dd/yyyy')}},
               @{Name='Owner';E=
                   {(($_.GetAccessControl().Owner.Split('\'))[1])}},
               FullName | Export-Csv 'H:\demo\scan1.csv' -NoTypeInformation
  
  PS H:\&gt; Get-ChildItemRecursive
  cmdlet Get-ChildItemRecursive at command pipeline position 1
  Supply values for the following parameters:
  FullName: H:\demo\
  
  Directory: H:\demo
  
  Mode                LastWriteTime     Length Name
  
  ----                -------------     ------ ----
  
  -a---         6/21/2017   4:12 PM     248472 lastrun.csv
  
  -a---         6/26/2017  11:27 AM        706 demo1.csv
  
  -a---         6/21/2017   1:38 PM       7861 4thrun06-21-17.csv
  
  -a---         6/21/2017  11:50 AM       2182 firstrun06-21-17.csv
  
  -a---         6/21/2017   2:41 PM       1334 demo.csv
  
  -a---         6/21/2017  12:24 PM      20985 3rdrun06-21-17.csv
  
  -a---         6/26/2017   2:24 PM          0 scan1.csv
  
  -a---         6/21/2017   4:22 PM       3671 sort-parent-subfolder.csv
  
  -a---         6/21/2017  12:25 PM       7298 2ndrun06-21-17.csv
  
  -a---         6/22/2017   4:46 PM       4637 2ndfolderRun6-22-17.csv
  
  -a---         6/22/2017  10:59 AM      28540 firstfolder.csv
  
  -a---         6/22/2017   4:59 PM     104618 4thfolder.csv
  
  
  PS H:\&gt;
  </code></pre>