Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Created February 15, 2016 21:53
Show Gist options
  • Select an option

  • Save FilBot3/df166198d6cae7089506 to your computer and use it in GitHub Desktop.

Select an option

Save FilBot3/df166198d6cae7089506 to your computer and use it in GitHub Desktop.
PowerShell Delete files older than x days
$limit = (Get-Date).AddDays(-2)
$path = "E:\SSP\AttachContent"
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment