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
86,128 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • 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. 
  • 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 
1 - 10 of 160 Items