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

  • Quotes
    1 Posts | Last post March 05, 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.
  • 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?
  • 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...
      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:
      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.
      Any idea why?
  • 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.
      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..
      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 -EmailFrom -EmailSmtpServer -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
  • Does not delete folders containg umlauts
    1 Posts | Last post November 02, 2017
    • Hi.
      I'm using this as a Clean up script and it works fine for all files and most folder. But we oprate in sweden and have quite a few folder on our share that contains äöå, ie characters with umlauts. The script does not delete theese folder (it delete files within them though)
      How can I work around this.
  • Extend Autolog feature to only retain specified # of logs
    2 Posts | Last post October 18, 2017
    • Great Script!!!  But is there an option to have it clean up after itself so that if the Autolog functionality is utilised then you do not end up with a zillion log files after a period of time?  An option to limit the # of autolog files to a specified amount would be good - or do you think this would be better in the .cmd file that launches the powershell script?
      Also - how would you include the onscreen output as the body of the email?
    • Hey M2Curly, I am by no means an expert but I have this problem solved in my script and I thought I would share it with you.  My script is a bit more involved than what I show here but this should work for you.  After you've tested it, remove the -ListOnly switch.
      Powershell.exe -executionpolicy remotesigned -Command "&{C:\MaintenanceBats\deleteold.ps1 -FolderPath 'C:\' -FileAge 14 -LogFile C:\Temp\GarbageLogs\log -AutoLog -ListOnly -VerboseLog -IncludePath 'C:\Temp\GarbageLogs' -ExcludePath 'C:\Users','C:\ProgramData','C:\Windows\SysWoW64','C:\SoftDist' -NoFolder}"
  • Delete one out of multiple files with the same extension.
    2 Posts | Last post October 18, 2017
    • Hi Jaap
      I have used your script and it works like a charm.
      I have one more need which is consider I have two ZIP files in a folder.
      Now I only want to delete from the folder and want to keep as it is.
      If I use IncludeFileExtension, it deleted all the zip files.
      Is there a way in which I can search for the file name also instead of the extension? Please let me know.
    • I also have a similar question.  The C:\Temp file collects a number of .txt log files.  The problem is, I don't want to delete ALL .txt files, just those that start with INF.  I tried using -IncludeFileExtension 'INF*.txt' but that didn't work.  Any help would be greatly appreciated.
  • Exclude specific file.
    1 Posts | Last post October 06, 2017
    • Hi
      Great script.
      Is it possible to exclude a specific file in a directory? As below, I can exclude a directory but I need to exclude a file in the root of the -FolderPath directory!
      -ExcludePath "\\domain\dfs\Client\GREEN\_SSH Keys","\\domain\dfs\Client\GREEN\MonitorAgent-DoNotDelete.txt"
1 - 10 of 166 Items