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.

deleteold.ps1
 
 
 
 
 
4.6 Star
(130)
101,542 times
Add to favorites
Storage
4/12/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • -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
    • 
      Hi,
      
      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.
      
      Thanks.
  • 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= "myemail3@gmail.com" 
      $EmailPass= "MyPass@123" 
      $EmailSmtpServer= "smtp.gmail.com"
      $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
      
      	Severity=Warning
      
      	SequenceNumber=15
      
      	HostName=ConsoleHost
      	HostVersion=4.0
      	HostId=7ea927bf-8dff-4a6d-948d-59b55cb300ff
      	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
      	EngineVersion=4.0
      	RunspaceId=8ca03d5e-dd45-4b4b-9976-a122afe50eda
      	PipelineId=1
      	CommandName=
      	CommandType=
      	ScriptName=
      	CommandPath=
      	CommandLine=
      
      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
  • Long path/filename not supported
    1 Posts | Last post February 16, 2018
    • Hi, great script but...
      
      I cannot make this script to work with long pathname (>248 characters) and long filename (>260 characters).
      
      Is it possible to make this script working with long directory path?
      
      Thanks 
      
      
      
      
       
1 - 10 of 174 Items