title: Delete folder X older than x days in power shell
body: <p>I'm using PowerShell script to delete folders which are older than x days. </p>
  
  <pre><code>$limit = (Get-Date).AddDays(-15)
  $path = "xxxx\path"
  
  # Delete files older than the $limit.
  Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
  
  # Delete any empty directories left behind after deleting the old files.
  Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse`
  </code></pre>
  
  <p>But it doesn't work always, and I couldn't figure out what's wrong. is there any other way I can delete certain folders for example if there are more then 5 folders..? or is it best to delete which one is older..? </p>
  
  <p>I do a backup via Powershell script which works great, but when I'm using this script to delete older backups some reason it doesn't work always.  </p>
  
  <p>is there any alternative way to do this.?</p>
  
  <p>Lot's of love humans</p>