title: Delete all files and folder working very slow
body: <p>I need to delete all the archived files and folder older than 15 days.</p>
  
  <p>I have implemented the solution using PowerShell script but it taking more than a day to delete all files. Total size of the folder is less than 100 GB.</p>
  
  <pre class="lang-cs prettyprint-override"><code>$StartFolder = "\\Guru\Archive\"
  $deletefilesolderthan = "15"
  
  #Get Foldernames for ForEach Loop
  $SubFolders = Get-ChildItem -Path $StartFolder |
                Where-Object {$_.PSIsContainer -eq "True"} |
                Select-Object Name
  
  #Loop through folders
  foreach ($Subfolder in $SubFolders) {
      Write-Host "Processing Folder:" $Subfolder
  
      #For each folder recurse and delete files olders than specified number of days while the folder structure is left intact.
      Get-ChildItem -Path $StartFolder$($Subfolder.name) -Include *.* -File -Recurse |
          Where LastWriteTime -lt (Get-Date).AddDays(-$deletefilesolderthan) |
          foreach {$_.Delete()}
  
      #$dirs will be an array of empty directories returned after filtering and loop until until $dirs is empty while excluding "Inbound" and "Outbound" folders.
      do {
          $dirs = gci $StartFolder$($Subfolder.name) -Exclude Inbound,Outbound -Directory -Recurse |
                  Where {(gci $_.FullName).Count -eq 0} |
                  select -ExpandProperty FullName
          $dirs | ForEach-Object {Remove-Item $_}
      } while ($dirs.Count -gt 0)
  }
  Write-Host "Completed" -ForegroundColor Green
  #Read-Host -Prompt "Press Enter to exit"
  </code></pre>
  
  <p>Please suggest some way to optimise the performance.</p>