You can delete files and folders recursively that are older than n number of days using PowerShell inbuilt cmdlets and pipe. Here is an example for your reference –
1 2 3 4 5 6 7 8 9 10 |
## Declare the location and time $n = 10 $path = "C:\Some\Path" $limit = (Get-Date).AddDays(-$n) ## Delete files older than the $n days. 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 |
Above script developed by: https://stackoverflow.com/users/602585/deadlydog