Skip to content

Instantly share code, notes, and snippets.

@Piket95
Last active January 12, 2024 10:28
Show Gist options
  • Save Piket95/5baf91bfa6b52f26783cbb95aec11c8e to your computer and use it in GitHub Desktop.
Save Piket95/5baf91bfa6b52f26783cbb95aec11c8e to your computer and use it in GitHub Desktop.
Powershell Script - Delete all files in the directory where the script is in that are older than 30 days
$items = Get-ChildItem ./ -Recurse
# check if every item found in the directory where this script is currently in is older than 30 days and delete them.
# Also exclude this script from results.
$removedItems = 0
$items.ForEach({
if( ($_.LastWriteTime -lt (Get-Date).AddDays(-30)) -and ($_.FullName -ne $PSCommandPath) ) {
$_ | Remove-Item
$removedItems++
}
})
# write how many files got deleted in this script execution iteration to a logfile in personal documents
echo "[$((Get-Date).ToString())] $(pwd): $($removedItems) files were removed" >> "<logfilepath>\DownloadTempDelete.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment