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
109,994 times
Add to favorites
E-mail Twitter Digg Facebook
  • Compare Modified,Accessed and Created
    1 Posts | Last post February 15, 2020
    • Is it possible to compare all 3 (Modified,Accessed and Created) and the script uses the most recent date? I tried running this script with the -LastAccessTime and -CompareCreateTimeLastModified switch but that doesn't seem to work.
      I'd run just the compareCreateTimeLastModified by itself but that would exclude files that haven't been modified but are accessed regularly. 
      I have examples of files that have a Date created of 2/14/2020 but a date accessed and date modified of 11/11/2019
  • IncludeFileExtension will not work in task
    2 Posts | Last post February 08, 2020
    • I'm having an issue using a scheduled task to perform some parts of this script. If I run -FolderPath "\\server\d$\temp" -FileAge 30 -NoFolder -IncludeFileExtension '.log*' -Logfile "\\server\d$\temp" -autolog -verboselog -ListOnly from the Powershell window, I correctly generate a logfile that lists the file with the .log extension that would be deleted. If I use the same argument in a scheduled task, the logfile agrees that I used the parameters in my call, but the section where the files should be listed is empty. For testing purposes, I am using the same user for both scenarios, and permission elevation is not an issue.
      Also, as os_car mentioned below, the IncludePath parameter is not working either, which led me to try the file extension route instead.
    • @Minister of Silly Walks
      I found that IncludePath and ExcludePath parameters can only accept one parameter and not the array of paths. If you provide comma separated list script consider it as next parameter value. Like if I provide 2 paths for parameter IncludePath, script will get 2nd path as ExcludePath and if provide 3 paths, scripts takes 3rd path for parameter ExcludeFileExtention. Need a fix.
  • 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?
1 - 10 of 180 Items