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.

deleteold.ps1
 
 
 
 
 
4.6 Star
(121)
83,331 times
Add to favorites
Storage
4/12/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • KeepFile and subfolders - not functional?
    1 Posts | Last post Mon 8:17 PM
    • 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
      
      
  • 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. 
  • Memory Problems and Ideas?
    1 Posts | Last post June 05, 2017
    • I have a very large volume to process and I noticed that your data intake of files is everything and then you carve things out. As you can imagine it's eating up ALL my ram to do this, plus taking forever to run.   Do you have a version out there that filters the input based on the criteria specified in the switches? 
      
      I know the solution is windows 2012 to do this natively, but am hoping to use your code for our 2008 server.   
      
      Thank you and much appreciated senor. 
  • Sub folders?
    1 Posts | Last post May 19, 2017
    • Hi. 
      I want to delete files in a specific subfolder folder in every folder like this D:\Users\*\Downloads 
      Am I able to do this with deleteold.ps1?
  • Sub folders?
    1 Posts | Last post May 11, 2017
    • We have a root folder (scanning) that contains subfolders for all the users (about 21). They NEVER clear out their scans and, as a result, we run out of disk space. I want to be able to run a script that will start at E:\Scanning and delete files older than 30 days from ALL the subfolders (e.g. E:\Scanning\Joe; E:\Scanning\Mary; etc.). I am new to PowerShell. is this type of action available using PowerShell?
  • Hi Jaap
    1 Posts | Last post May 08, 2017
    • in following your instructions I am passing this syntax 
       .\deleteold_files.ps1 -FolderPath  G:\IVYDBEurope_Test_Loader\IvyDBInstaller\data\eu\patch -LogFile G:\IVYDBEurope_Test_Loader\IvyDBInstaller\data\eu\scripts\log AutoLog -KeepFil
      e '5' -FileAge '100' -CreateTime  -IncludeFileExtension '*.zip' -IncludePath G:\IVYDBEurope_Test_Loader\IvyDBInstaller\data\eu\patch
      
      and there are alot of files in this directory but it is not removing them why ?
      
      Thank you 
      
      BTG_Guy
      
      
  • Scheduled Tasks
    1 Posts | Last post April 24, 2017
    • This script runs perfect when I do it from a powershell window.  However, when I run it as as scheduled tasks, it finishes immediately and no files get deleted and the log files does not get updated.  I have the Tasks setup like all my others scripts that I run and I can't figure out why this one doesn't work.  
  • Exclude files
    1 Posts | Last post March 30, 2017
    • Hi Jaap,
      
      Many thanks for this script. After some testing just one Q namely:
      
      We are using the following command:
      .\deleteold.ps1 -folderpath C:\ -fileage -1 -log c:\log.txt -autolog -verboselog -ListOnly -IncludePath "C:\Users\","C:\Windows\Temp\","C:\Windows\Prefetch\" -IncludeFileExtension '.pf*','.log*','.iso*','.tmp*','.bak*','.dat*','.old*','.~??*','.chk*'
      
      The execution works great and provided us the correct output.
      
      But, we want to exclude some files using above command:
      OPA11.BAK
      OPA11.DAT
      WFCWIN32.LOG
      ISCTX.LOG
      
      Is this possible, if Yes... Are you able to guide me here?
  • FolderPath Several Servers
    1 Posts | Last post March 15, 2017
    • Hi Jaap, thank you for the script.
      I have 3 servers with the same Path, for ex., \\servername1\temp , \\servername2\temp.
      How can I integrate all 3 server paths for running the script once? 
      thanks. 
1 - 10 of 157 Items