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
(128)
98,356 times
Add to favorites
Storage
4/12/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • 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 15, 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 
      
      
      
      
       
  • Help with batch file launching
    2 Posts | Last post February 13, 2018
    • Thanks for this.
      
      I want to run this from a batch file.  I currently have a scheduled task that runs when my users logon.  It runs a batch file in the "Scripts" folder in SYSVOL.  It has to clean the logged on users temp folder (%temp%) based on creation date.
      
      Do I just copy the script into the same "Scripts" folder and use following statement in the batch file?   PowerShell.exe deleteold.ps1 -FolderPath %temp% -FileAge 2 -CreateTime
      
      Will this work when a user logs on without local admin rights?
      
      Will it recognize %temp%?
      
      Will Powershell error because users can't run Powershell (many times when I try to do powershell scripts they fail because of execution policy)?
      
      I don't want any log files created so I left out the parameter.  Is this correct?
    • I found that it does recognize %temp% but I'm having difficulty calling it from a batch file.  Using the included example...
      
      .EXAMPLE 
      PowerShell.exe deleteold.ps1 -FolderPath '%temp%' -FileAge 3 -LogFile %temp% -AutoLog 
      
      I'm getting an error... The term 'deleteold.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
  • Date Modified or Date Created?
    2 Posts | Last post January 13, 2018
    • Does this delete using the files "Date Modified" or "Date Created"?  I would like to delete files according to creation date.
    • You can use the -CreateTime switch.
  • Running as a scheduled task...
    1 Posts | Last post December 29, 2017
    • Hi, thanks for this script!
      I just have one issue I was hoping you could assist with..
      
      It works perfectly when I run the script from a PS CL.
      I include the parameter:
      -ExcludePath "E:\FTP-Homes\roram\","E:\FTP-Homes\Peleton\"
      
      ......and when I use "-ListOnly", it works and I see this in the logs:
      -ExcludePath
      			E:\FTP-Homes\roram\
      			E:\FTP-Homes\Peleton
      
      However, when I pass the parameter in the task, (for example:  -Command "& c:\scripts\deleteold.ps1 -FolderPath E:\FTP-Homes -ListOnly -ExcludePath "E:\FTP-Homes\roram\","E:FTP-Homes\Peleton\"")  (shortened for clarity.)
      
      It fails and show this in the log file.
      -ExcludePath
      			E:\FTP-Homes\roram,E:\FTP-Homes\Peleton
      
      Any idea why?
      Bob
      			
  • Send mail to all users only after deletion
    1 Posts | Last post December 11, 2017
    • Thanks for this great script.
      We have a non-persisiten VDI windows 7 desktop enviroment(vMware).
      We created a share for every user for big downloads and files.
      \\Server\share\%username%
      
      I want to Configure the script to delete files after 14 days.
      Afterwards send a mail if files/folders are deleted. Something like:
      
      Dear $Firstname,
      The files you see in the Logfile have been deleted blablabla..
      blabla
      Regards
      Servicedesk
      
      A mail with this meassage in English and Dutch.
      It's probably possible with this script, but it's a bit obscure to put all that in the command line within taskscheduler.
      I don't want to send a mail everyday to the users. Only when files and folders are deleted.
      
      The syntax i'm testing:
      PowerShell.exe -noprofile -executionpolicy remotesigned -Command "&{ C:\Install\Scripts\deleteold.ps1 -FolderPath '\\dc01\FILES-DELETED-AFTER-14-DAYS\$env:username\' -FileAge 14 -Logfile '\\dc01\FILES-DELETED-AFTER-14-DAYS\$env:username\' -ListOnly -AutoLog -VerboseLog -CleanFolders -CreateTime -EmailSubject 'FILES AND FOLDERS DELETED AFTER 14 DAYS' -EmailTo ServiceDesk@ourdomain.com -EmailFrom Account@ourdomain.com -EmailSmtpServer smtp.ourdomain.com -EmailBody 'Test EmailBody'}"
      
      1) How could i send a mail to users, only after the files and folders are deleted in the %username% folder ? And a CC to the service desk?
      2) Can't use the $Firstname parameter?
      3) The log files is not created if i use a drive letter instead of the UNC the log is created, and the script runs succesfully.
      4) The syntax doesn't seems to work in Task Scheduler. Probably quotian mark issue.
      5) Also like to cleanup de logfiles. If i test -IncludePath 'C:\Temp\', nothing is deleted.
      
      Any help will be appreciated
      Regards
      Steve
1 - 10 of 170 Items