title: PowerShell find non-empty UNC path folders works but is very slow
body: <p>In a nutshell, I have about 30 folders called Document(s) and a number, so</p>
  
  <p>Document<br>
  Documents2<br>
  ...<br>
  Documents30  </p>
  
  <p>Below those folders, potentially, is a client folder (which can be in multiple Document(s) folders). When I'm archiving this data, I need to find non-empty folders to Robocopy over to another location.</p>
  
  <p>This script will search all of the Document(s) folders for the correct client name, and then check that the folders below it aren't empty. But it's slow. Really slow. Any suggestions to speed this up? </p>
  
  <p>I'm not sure if PS counts all files, or just checks that the count is > 0 and moves on. Other attempts I made to check that root folder size > 0 did not run well either; they seemed to also want to count/sum every file/folder, when I really just need to know what's > 0 bytes.</p>
  
  <p>Thanks</p>
  
  <pre><code>$filePath =  "\\path\to\Document*\*"  
  $folderName =  "HelloWorld"
  
  Get-ChildItem -Force $filePath -ErrorAction SilentlyContinue | 
  Where-Object { ($_.PSIsContainer -eq $true) `
  -and ( $_.Name -like "*$folderName*") `
  -and (Get-ChildItem -Path $_.FullName -Recurse | 
                  Where-Object {$_.PSIsContainer -eq $True `
                      -and $_.GetFileSystemInfos().Count -gt 0 })  } |
  Select-Object FullName | format-Table * -AutoSize 
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>For @TheMadTechnician:</p>
  
  <p>Is this correct to return the 'root' folder path for anything with non-empty subdirectories? e.g.   </p>
  
  <p>\\path\to\Documents3\HelloWorld<br>
  \\path\to\Documents19\HelloWorld<br>
  \\path\to\Documents20\HelloWorld  </p>
  
  <pre><code>$FolderName = "HelloWorld"
  $FilePath = "\\path\to\Document*\$FolderName"
  
  $EmptyPaths = @()
  ForEach($Folder in (Get-ChildItem $FilePath -Directory)){
      If((Get-ChildItem "$($Folder.FullName)\*").Count -gt 0){Continue}
      Get-ChildItem $Folder -Recurse -File|ForEach{break}
      $EmptyPaths += $Folder |
      Select-Object FullName | format-Table * -AutoSize 
  } 
  </code></pre>