Skip to content

Instantly share code, notes, and snippets.

@bradymholt
Created March 6, 2015 01:49
Show Gist options
  • Save bradymholt/31aac80b1649a6111f50 to your computer and use it in GitHub Desktop.
Save bradymholt/31aac80b1649a6111f50 to your computer and use it in GitHub Desktop.
Deletes directories older than specified number of days
param ([Parameter(Mandatory=$True)] $clean_path, [Parameter(Mandatory=$True)] $search_pattern, [Parameter(Mandatory=$True)] $max_days)
# get the current date
$curr_date = Get-Date
# determine how far back we go based on current date
$del_date = $curr_date.AddDays(-($max_days))
Get-ChildItem $clean_path | Where {$_.Attributes -eq 'Directory'} | `
Foreach-Object{
Write-Host $_.FullName
Get-ChildItem $_.FullName | Where {$_.Attributes -eq 'Directory'} | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item -Recurse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment