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

  • 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 
  • 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:
      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? 
  • Extend Autolog feature to only retain specified # of logs
    1 Posts | Last post February 21, 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?
  • RegExPath Query
    1 Posts | Last post January 27, 2017
    • Hi, 
      Great script.
      I am attempting to run the scrip on a folder structure where we have lots of duplication of the same folder.
      I want to delete all files and folder older than x days but Exclude and not delete anything from the INCOMING folder, this folder exists as a sub folder for may root folders. (e.g. \\FQDN\DFS\Client\Client1\Incoming\..., \\FQDN\DFS\Client\Client2\Incoming\...
      I have -RegExPath -ExcludePath "(?:^|\W)INCOMING(?:$|\W)"
      but running with -Listonly provide 0 results?
      Pleases can you help?
  • Log file on network share
    7 Posts | Last post January 24, 2017
    • I was able to run this script and does what's expected except it will not write the log file to the destination specified. The destination is in a folder called logs in the FolderPath itself. This wasn't an issue when selecting a local destination.
      The odd thing is I have Full access(read/write/delete) to that destination in Windows Explorer. No errors were returned when running the script, in fact the output page even shows the expected log file but not written at all.
        Deleteold     ::      Script to delete old files from folders
         Started  :   3/1/2016, 4:40:36 PM
         Folder   :   \\Shared\Public
         Switches :
                      -FileAge 7
      Question: Any ideas why logs aren't working for this instance?
      I have the script run via a batch script as per below
      @ECHO OFF
      PowerShell.exe -Command "& '%~dpn0.ps1'-FolderPath '\\Shared\Public' -FileAge '7' -LogFile '\\Shared\Public\Logs\log.log' -CleanFolders -AutoLog -VerboseLog"
    • I have this same issue, any workaround or fix coming up?
    • I have attempted to reproduce the issue but I cannot reproduce this with the given parameters. Can you update the batch file to the following:
      @ECHO OFF
      PowerShell.exe -Command "& '%~dpn0.ps1'-FolderPath '\\Shared\Public' -FileAge '7' -LogFile '\\Shared\Public\Logs\log.log' -CleanFolders -AutoLog -VerboseLog;$Error"
      To see if there are any errors? Because my experience is that if it doesn't log to a disk it should be generating a lot of errors.
    • Hi Jaap,
      I'm having the same problem, I test it using part of the code to output the log file like this :
      $test = "Test string"
      Write-Output $test | Add-Content -LiteralPath \\ComputerName\ShareName\Test.txt
      it doesn't create Test.txt, but if I removed the -LiteralPath, it works
      $test = "Test string"
      Write-Output $test | Add-Content \\ComputerName\ShareName\Test.txt
      it created the test.txt in the destination folder, is it related to my environment ?
      I have read and write access to the shared folder.
    • Hi Jaap, I am still looking for a fix or workaround for this problem. Thanks!
    • I have not been able to reproduce this issue, maybe I missed something. Can you give me the exact code you are using the execute this script, that way I can investigate this further. This script is overdue for another update so I will pick this up, hopefully have this resolve before the end of the month.
      Jaap Brasser
    • Hi Jaap, thanks for replying, here is the command line and some output to console
      powershell \\CDC-met-nas-QA\QA\8216\\Installation\BatchScripts\DeleteOld.ps1 -FolderPath \\CDC-met-nas-QA\QA\8216\PFI\REGIONS\PROD\Data\JobOutput -fileage 100 -nofolder -CompareCreateTimeLastModified -logfile \\CDC-met-nas-QA\QA\8216\AppLogs\ -autolog -verboselog
        Deleteold	::	Script to delete old files from folders
         Started  :   1/23/2017, 4:53:11 PM
         Folder   :	\\CDC-met-nas-QA\QA\8216\PFI\REGIONS\PROD\Data\JobOutput
         Switches :	
      		-FileAge 100
         Retrieving list of files and folders from: \\CDC-met-nas-QA\QA\8216\PFI\REGI
         Files	: 544
         Folders	: 0 
         Old files	: 0
         Starting with removal of old files...
         Finished deleting files
         Check/remove empty folders started...
         Empty folders deleted
         Files               : 0
         Filesize(MB)        : 0
         Files Failed        : 0
         Failedfile Size(MB) : 0
         Folders             : 0
         Folders Failed      : 0
         Finished Time       : 1/23/2017, 4:53:12 PM
         Total Time          : 00:00:01
      ErrorCode: 0
      WARNING: Log File was not provided.
1 - 10 of 154 Items