Delete files older than x-days - Cleanup Script

Script to delete files older than x-days. The script is built to be used as a scheduled task, it automatically generates a log file based on the copy location and the current date/time. There are various levels of logging available.

4.6 Star
89,701 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Send mail to all users only after deletion
    1 Posts | Last post December 11, 2017
    • Thanks for this great script.
      We have a non-persisiten VDI windows 7 desktop enviroment(vMware).
      We created a share for every user for big downloads and files.
      I want to Configure the script to delete files after 14 days.
      Afterwards send a mail if files/folders are deleted. Something like:
      Dear $Firstname,
      The files you see in the Logfile have been deleted blablabla..
      A mail with this meassage in English and Dutch.
      It's probably possible with this script, but it's a bit obscure to put all that in the command line within taskscheduler.
      I don't want to send a mail everyday to the users. Only when files and folders are deleted.
      The syntax i'm testing:
      PowerShell.exe -noprofile -executionpolicy remotesigned -Command "&{ C:\Install\Scripts\deleteold.ps1 -FolderPath '\\dc01\FILES-DELETED-AFTER-14-DAYS\$env:username\' -FileAge 14 -Logfile '\\dc01\FILES-DELETED-AFTER-14-DAYS\$env:username\' -ListOnly -AutoLog -VerboseLog -CleanFolders -CreateTime -EmailSubject 'FILES AND FOLDERS DELETED AFTER 14 DAYS' -EmailTo -EmailFrom -EmailSmtpServer -EmailBody 'Test EmailBody'}"
      1) How could i send a mail to users, only after the files and folders are deleted in the %username% folder ? And a CC to the service desk?
      2) Can't use the $Firstname parameter?
      3) The log files is not created if i use a drive letter instead of the UNC the log is created, and the script runs succesfully.
      4) The syntax doesn't seems to work in Task Scheduler. Probably quotian mark issue.
      5) Also like to cleanup de logfiles. If i test -IncludePath 'C:\Temp\', nothing is deleted.
      Any help will be appreciated
  • Does not delete folders containg umlauts
    1 Posts | Last post November 02, 2017
    • Hi.
      I'm using this as a Clean up script and it works fine for all files and most folder. But we oprate in sweden and have quite a few folder on our share that contains äöå, ie characters with umlauts. The script does not delete theese folder (it delete files within them though)
      How can I work around this.
  • Extend Autolog feature to only retain specified # of logs
    2 Posts | Last post October 18, 2017
    • Great Script!!!  But is there an option to have it clean up after itself so that if the Autolog functionality is utilised then you do not end up with a zillion log files after a period of time?  An option to limit the # of autolog files to a specified amount would be good - or do you think this would be better in the .cmd file that launches the powershell script?
      Also - how would you include the onscreen output as the body of the email?
    • Hey M2Curly, I am by no means an expert but I have this problem solved in my script and I thought I would share it with you.  My script is a bit more involved than what I show here but this should work for you.  After you've tested it, remove the -ListOnly switch.
      Powershell.exe -executionpolicy remotesigned -Command "&{C:\MaintenanceBats\deleteold.ps1 -FolderPath 'C:\' -FileAge 14 -LogFile C:\Temp\GarbageLogs\log -AutoLog -ListOnly -VerboseLog -IncludePath 'C:\Temp\GarbageLogs' -ExcludePath 'C:\Users','C:\ProgramData','C:\Windows\SysWoW64','C:\SoftDist' -NoFolder}"
  • Delete one out of multiple files with the same extension.
    2 Posts | Last post October 18, 2017
    • Hi Jaap
      I have used your script and it works like a charm.
      I have one more need which is consider I have two ZIP files in a folder.
      Now I only want to delete from the folder and want to keep as it is.
      If I use IncludeFileExtension, it deleted all the zip files.
      Is there a way in which I can search for the file name also instead of the extension? Please let me know.
    • I also have a similar question.  The C:\Temp file collects a number of .txt log files.  The problem is, I don't want to delete ALL .txt files, just those that start with INF.  I tried using -IncludeFileExtension 'INF*.txt' but that didn't work.  Any help would be greatly appreciated.
  • Exclude specific file.
    1 Posts | Last post October 06, 2017
    • Hi
      Great script.
      Is it possible to exclude a specific file in a directory? As below, I can exclude a directory but I need to exclude a file in the root of the -FolderPath directory!
      -ExcludePath "\\domain\dfs\Client\GREEN\_SSH Keys","\\domain\dfs\Client\GREEN\MonitorAgent-DoNotDelete.txt"
  • Date Modified or Date Created?
    1 Posts | Last post September 27, 2017
    • Does this delete using the files "Date Modified" or "Date Created"?  I would like to delete files according to creation date.
  • KeepFile and subfolders - not functional?
    2 Posts | Last post September 15, 2017
    • Hello Jaap - this tool is *almost there* for my needs. I have 1 folder with 530 subfolders. Those 530 subfolders also have 230 subfolders below them. I need to use the KeepFile 1 in the script in order to retain only the newest file in a folder or subfolder if ALL files meet the delete criteria. Unfortunately, it appears that KeepFile will only work on the top folder level and does not dive into the subfolders unless I create the command manually for each.
      PS C:\Users\administrator> .\deleteold.ps1 -FolderPath "S:\ExportArchive\Accel" -KeepFile 1 -FileAge 350 -LogFile U:\logs -AutoLog
      In just that path alone, there are nearly 100 files that could be removed if subfolders are processed. Unfortunately, the command as presented above provides 0 files and folders to remove.
      Is there a way to force the review of subfolders without writting the command manually? I would love to run this tool at the topmost folder and allow it to "do its thing" without my interaction.
      Thank you!
      Terry Vos
    • My issue with this script was resolved by Spiceworks -
  • Special characters in Path not recognised
    1 Posts | Last post September 04, 2017
    • Hi,
      I have a path that contains the '&' character and this is causing the script to fail reporting that the path up to the point of the & is not found.
      Is there any way around this?
  • Great script
    1 Posts | Last post August 03, 2017
    • Thanks a lot. You save may day. This is the great script!
  • Script Error
    1 Posts | Last post July 10, 2017
    • I currently enter this line of code below the "#>" and before the "#region Parameters"
      .\deleteold.ps1 -FolderPath C:\Users\username\Desktop\Delete_Test\ -FileAge 10 -ListOnly -LogFile C:\Users\username\Desktop\Delete_Test\log.log 
      I am currently getting a lot errors such as "The script failed due to call depth overflow.", "The term 'param' is not recognized as the name of a cmdlet, function, script file...."
      I am not sure where I am supposed to enter in the script the ".\deleteold.ps1, etc"
      Any help is greatly appreciated. 
1 - 10 of 162 Items