I used excellent script Delete files older than x-days – Cleanup Script which requires bunch of parameters.
.\deleteold.ps1 -FolderPath "D:\test" -FileAge 7 -LogFile "D:\log.txt" -autolog -VerboseLog -CleanFolders
Most important parameters:
The path that will be recursively scanned for old files.
Filter for age of file, entered in days. Use -1 for all files to be removed.
Automatically generates file name at path specified in -logfile. If a file name is specified in the LogFile parameter and the AutoLog parameter is used only the path specified in LogFile is used. The file name is created with the following naming convention:
Logs all delete operations to log, default behavior of the script is to log failed only.
If this switch is specified any empty folder will be removed. Default behavior of this script is to only delete folders that contained old files.
Creating a task in Task Scheduler
Just give it a name
Use schedule when you want to run it
Check what PowerShell version you have and update the path
-Command "& d:\scripts\deleteold.ps1 -FolderPath "D:\test" -FileAge 0 -LogFile "D:\log.txt" -autolog -VerboseLog -CleanFolders; exit $LASTEXITCODE"
exit $LASTEXITCODEis used to get exit code from the script
O learnt this from Dmitry’s