Powershell script to delete old files

This is a simple but useful script. After a lot of questions regarding PowerShell scripts to clean up old backup files I decided to put it out here.You can simply edit anything you like in the PowerShell file the execute code is only a few lines, the variable $oldTime is how old

 
 
 
 
 
(26)
9,601 times
Add to favorites
1/9/2013
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Script runs but no files are deleted.
    2 Posts | Last post May 27, 2013
    • Hi, Script looks great, but it will not delete the files in the directories I have specified, the script seems to run (Out put to screen etc..) but the files remian in the respective directories.  All I get is the comments in the PS shell "trying to delete the files in dir......."
      
      What might I be doing wrong?
      
      I made sure the extensions for the files I want to delete are correct, but even though I have put files in there that are over 5 days (My modification) and with extensions I have included the files do not get deleted.
      
      Any help is most appreciated.  The script looks awesome, just need to get it functioning.
      
      Thanks
    • Nevermind, I figured out what I was doing wrong.  So simple... amazing what you can see when you have left your work for a day, got some sleep and come back fresh :).
      
      Awesome script Tord, WD
      
      Rogue_Script
  • delete folders
    1 Posts | Last post May 13, 2013
    • Hi Tord. 
      This scrip deleted also folders which contains files earlier than $oldTime. 
      Fore example folder older than certain date, but some files in this folder earlier.
      Have you managed to write script to don't delete such files?
  • delete files but not folders
    1 Posts | Last post April 18, 2013
    • Hey-- I'm trying to use this as a scheduled task. It runs every night, but I want it to just delete files, not folders. Unfortunately, all the files don't have an extension, so I have taken out the include. I think if I put the include back in with *. , it will still delete the folders as well, yes?
      
      Is there a way to have this just delete 
      c:\temp\file
      c:\temp\otherfile
      
      but not delete
      
      c:\temp\directory
      c:\temp\otherdirectory
      
      given c:\temp in the pathlist?
  • LastWriteTime
    1 Posts | Last post April 17, 2013
    • Thank you Brian that helped in my situation with copying of files to a different location.
      Thank you to all for this.
  • A slight modification of interest
    1 Posts | Last post March 26, 2013
    • Here is a small change I made in your script so that it checks the files' last Modified time instead of their Creation time - if anyone is interested:
      
      Get-ChildItem $path -Recurse -Include "*.xml" | WHERE {($_.LastWriteTime -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force
  • Delete Folders?
    1 Posts | Last post February 04, 2013
    • Hi Tord,
      
      This script and many others I've seen are great for deleting the contents of folder, but what if I want to delete the contents and old folders? 
      
      We do a lot of terminal services profile cleanup, and the easy way is to rename the folder with a .old at the end. When the user logs in, a GPO recreates the new TS folder automagically. I want to delete all folders and files recursively from a certain path named <anything>.old greater than x days.
      
      I would run this as a scheduled task on the profile server...
  • Running script
    2 Posts | Last post January 09, 2013
    • Hi Tord,
      
      Do we need to put this script on task scheduler to run everytime after the number of days the files have been there or once you run this script it will delete the particular files in folder by itself recursively after number of days?
    • Yes you can run this as a schedule task if you want to. based on the threshold in the file you can set the age of the files that needs to be deleted, so if the file in the sample script here is 30days or older it will be deleated, it also crawls subfolders so for instance if I had folders like this:
      
      c:\temp\log\1
      c:\temp\log\2
      c:\temp\log\2\2
      
      and i add the c:\temp\log into the scripts text file it would crawl all the subfolders to delete all files older then 30 days in these folders.
      
      Hope this helps. Let me know if you need more help.
  • What is the recommend steps in the backup job?
    2 Posts | Last post September 16, 2012
    • I have a client that is using this script, but the replaced the 4th step of the Backup BizTalk Server job with this script, doing a health check on their environment I found that they had all the backup history still in the backup history table in the management database, because they replaced the 4th step that cleans up the backup history with this powershell script.
      
      What is your recommendation for adding this to the Backup BizTalk Server job?
      
      Bill
    • Hello Bill!
      
      My recommendation would be to edit the DaysToKeep in the Backup job found under the SQL Agents, I would not use this script to clean up BizTalk Backup logs, rather logs created for backing up Procsesed or failed files going through BizTalk. =)
  • A minor error
    2 Posts | Last post June 21, 2012
    • In the script you are opening a file called appList.txt that should be Pathlist.txt. I think. Otherwise it is awesome that you can do this in just 4 lines. And this works for UNC paths
      Regards Svavar
    • So true, I'll resolve that ASAP. Thanks for letting me know and the feedback!
  • for each deleting it is prompting to enter Yes or No
    4 Posts | Last post February 29, 2012
    • Hi Nordahl,
      
      First of all thanks for the scripting and it is useful for me to do some activities. But whenever i am executing this script in huge data store to delete for some GB's of files it prompting a confirmation in powershell screen.
      
      basically without man power the activity will done the scheduled time. But using this a man need to give a y for all the confirmation. Kindly help me and teach me to modify some lines to avoid this prompting.
      
      Thanks a lot,
      சிவசங்கரன்(Shivashankaran)
    • You should only get this warning when you try to delete folders, and theres a reason for that some folders shouldn't be deleted, make sure you dont have the extension .out or .xml on any folders, if you do BizTalk will try to delete it but will prompt you. However if you dont want to be prompt at all.
      Change this line (line: 7):
      Get-ChildItem $path -Recurse -Include "*.out", "*.xml" | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force 
      
      into
      Get-ChildItem $path -Recurse -Include "*.out", "*.xml" | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Recurse
    • Hi Nordahl,
      
      After using the attribute -recurse now we didn't get that prompt during the script execution. Once again thanks a lot for the script.
      
      Thanks a lot,
      சிவசங்கரன்(Shivashankaran)
    • Anytime Sankaran! :)