Skip to content

Instantly share code, notes, and snippets.

@timgaunt
Created January 28, 2016 13:17
Show Gist options
  • Save timgaunt/f1990598877f2f5e0b85 to your computer and use it in GitHub Desktop.
Save timgaunt/f1990598877f2f5e0b85 to your computer and use it in GitHub Desktop.
$ago = 7;
$now = Get-Date;
$limit = $now.AddDays($ago * -1)
$filename = "UmbracoTraceLog.txt.*"
$path = "D:\Websites\"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastwriteTime -lt $limit -and $_.name -like $filename } | Remove-Item -WhatIf
# 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 -WhatIf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment