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

  • EmailTo muliple emails not working
    1 Posts | Last post January 16, 2020
    • I wanted to send the email to multiple people, so I tested with my work and personal email addresses, and it worked.
      [string[]] $EmailTo=("",""), # Added default parameters
      Before putting this script into Production, I decided to test it against a test server and added additional people to the EmailTo and it only sends it to my work email.
      I put back only my work and personal email addresses and now it only sends it to my work email.
      What could I be doing wrong?
  • Great script
    2 Posts | Last post December 17, 2019
    • Thanks a lot. You save may day. This is the great script!
    • No doubt this is a great script. Blessed to be here.
  • Script not working as expected
    1 Posts | Last post October 23, 2019
    • Hi, I am trying to delete older files in two folders, so I am running the following:
      DeleteOld.ps1 -FolderPath H:\myScripts -FileAge 30 -IncludePath "H:\myScripts\VBScript","H:\myScripts\CMD" -LogFile H:\ -AutoLog
      And it doesn't delete anything.
      Am I doing it wrong?
  • KeepFile option being ignored
    2 Posts | Last post September 27, 2019
    • hi, i was using successully this option on a windows 2008 server. On Windows 2016 it appears bo be ignored. This is the output of the command wxecuted in list only. The KeepFile option is "read", but ignored.
      The script finds 8 files. I want 12 to be kept, but it will delete 4. Why?
      Please help me...
      Started  :   9/27/2019, 10:53:28 AM
       Folder   :   W:\test\PR
       Switches :
                    -FileAge 3
                    -KeepFile 12
            *** Running in Listonly mode, no files will be modified ***
       Retrieving list of files and folders from: W:\SAP\PR2
       Files        : 8
       Folders      : 1
       Old files    : 4
       Listing files...
       Finished listing files
       Files               : 4
       Filesize(MB)        : 1102528
       Files Failed        : 0
       Failedfile Size(MB) : 0
       Folders             : 0
       Folders Failed      : 0
       Finished Time       : 9/27/2019, 10:53:28 AM
       Total Time          : 00:00:00
    • P.S.: i am using Version: 2.0.5
  • How to submit a proposed update?
    1 Posts | Last post September 12, 2019
    • I wanted to include a friendly name in the From email field but found it would not except the format "friendlyname <>" because the "<" was reserved.  I added a parameter -EmailFromFriendly.
      I wanted the dynamic subject that gaves timestamps and folder but I wanted to customize it.  Providing a full subject with the -EmailSubject parameter does not allow access to the timestamps or folder.  I created a new -EmailSubjectParameter.
      I customized the code to use the parameters and added comments and an example.
      Can I submit my revision for your review?
      Thanks for the great tool!
  • Can force it to keep directory if all files are older than specified FileAge
    2 Posts | Last post March 13, 2019
    • What I didn't realise is if all files in the directory are older than the FileAge specified, it also removes the directory.
      How do I keep the directory intact ? (currently investigating script to see if it's embedded somewhere)
    • use the -NoFolder option
  • -Keepfile only works if there are more than that amount of files in the directory
    1 Posts | Last post October 30, 2018
    • -Keepfile only works if there are more than that amount of files in the directory. Otherwise it deletes them all. ex. if you have 5 files in a directory and you're -keepfiles is set at 6, it will delete them all. 
  • if folder doesn´t excist, the script looks one level up
    1 Posts | Last post September 17, 2018
    • Hi,
      I noticed that when i run the script with one folder:
      c:\script\deleteold.ps1 -FolderPath c:\logs2 -FileAge 14 -LogFile c:\script\log2.txt
      If c:\logs2 doesn't excist the script looks in c:\ and gives me a report of files/folders in the log2.txt 
      Is there any way I can make sure that the script doesn't do anything if a folder is missing?
  • KeepFile keeps oldest files first
    1 Posts | Last post August 14, 2018
      When using the KeepFile parameter, the command is actually keeping the oldest files first, not the newest files. Is it the desired behavior? On my part the proper behavior would be to keep the newest files, not the oldest files.
      See line 1045 to 1052:
                  $FileList = @($_.Group | Sort-Object -Property CustomTime     | Select-Object -Last ($_.Count-$KeepFile))
      Since the list is sorted by date and the -Last switch will keep the newest from the list, the oldest will be removed from the list, thus will be kept from being removed afterwards.
      This could be resolved easily by using the -First switch instead of the -Last switch.
  • EmailBody parameter
    1 Posts | Last post August 08, 2018
    • Greetings,
      How can I use this parameter to grab information from the log file created and send it as emailbody instead of attachment?
      Thanks you
1 - 10 of 178 Items