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

  • 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
  • Long Path Issues
    1 Posts | Last post May 02, 2018
    • What can we do to use this script with long path names? And we cannot shorten the path...
      FAILED ACCESS	The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. 
      Thank you!
  • problem with IncludeFileExtension
    1 Posts | Last post April 17, 2018
    • Hi,
      first of all. GREAT SCRIPT ! Thanks !!!
      Everything works fine but when I add: 
      -IncludeFileExtension '.imported' 
      it does not work. No files are matched. Also '.imp*' does not work. Could it be that there is a problem with file extension longer than three chars ?
      Best regards
  • Quotes
    2 Posts | Last post April 03, 2018
    • Thanks for this great script. It really does what I was looking for.
      Your documentation and examples say that I need to " for the IncludePath and ExcludePath. However when I use " it does not work for directories with spaces in the name. When I use ' I can exclude and include directories with spaces in the name.
    • Hello
      You can use "" for directories with spaces in the name, i.e: "C:\Program Files"
      .\deleteold.ps1 -FolderPath "C:\Program Files"  -FileAge 30 -ListOnly -LogFile E:\TempPath\Logs -AutoLog -VerboseLog
      Best regards
  • Send mail options
    1 Posts | Last post April 02, 2018
    • Hello
      I need to send email with script results, but when this script try to send show authentication error on Mail Server. Can you modify variables for it? for example:
      $EmailFrom= "" 
      $EmailPass= "MyPass@123" 
      $EmailSmtpServer= ""
      $EmailSmtpPort= "587"
      Thank you
  • Access denied to path
    2 Posts | Last post April 02, 2018
    • Can you help me? I'm trying to use this script with:
      .\deleteold.ps1 -FolderPath E:\Usuarios\Jairo.Celis\Documents\Prueba  -FileAge 30 -ListOnly -LogFile E:\Usuarios\Jairo.Celis\Documents\Prueba
      And show error message:
      Access to the path 'C:\Users\administrador.ET\Documents' is denied
      	HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE C:\Users\administrador.ET\Documents\deleteold.ps1 -FolderPath C:\Users\administrador.ET\Documents\Prueba -FileAge 30 -ListOnly -LogFile C:\Users\administrador.ET\Documents
      I has to added user account to full permissions on the path. Then, I try with admin user and show same error.
      What Can I?
      Thank you
    • Please ignore my question. thanks
1 - 10 of 174 Items