Last active
January 12, 2024 10:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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