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

4.4 Star
16,277 times
Add to favorites
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question

  • Delete old SQL backup across the server......
    3 Posts | Last post March 20, 2018
    • Hi,
      Is there possibility to create the same script with out any location specific.
      i.e. the script check the complete drives/folders/sub folders for any older SQL backup files and delete them of they are 30 days older.
      I want to create script that check all SQL backup files older then 30 days across the server and delete them if the condition is true.
      Please help me out on this if possible.
    • You can add multiple folders if you want.. :)
    • Hi Tord..
      Great Script it work perfectly for me..
      But how can execute this for multiple boxes?
  • Output to a log file?
    1 Posts | Last post September 28, 2016
    • Hi Tord,
      Thanks for this script, it has been helpful.
      I am trying to write the output to a log file so I can see the results.  On line 7 I added | Out-File deleted.txt
      It creates the file, but the files deleted are not added.  Any idea what I need to change?
  • task termination
    1 Posts | Last post October 27, 2015
    • Greetings,
      I'm new to scripting. I've got this set up as a scheduled task and it seems to stay in a running state. I would like, for me, the script to run and exit. I don't want it to continue scanning. In my task scheduler settings, I have it to terminate after an hour. I would like some guideance on the task terminating on its own.
  • delete files but not folders
    3 Posts | Last post September 08, 2015
    • 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 
      but not delete
      given c:\temp in the pathlist?
    • you need that last slash to not delete the directory, and the include to only delete the files you want.
      You can however make a crawler to resolve this?
    • If you're wanting to avoid deleting directories, I check the .PSIsContainer property to make sure that's not set.. If you're always doing this and never directories it's good safety. (also I move not delete here):
      Get-ChildItem $logdir | Where-Object {((-not $_.PSIsContainer) -and ($_.LastWriteTime -lt $OldestDate))} | Move-Item -Destination $archivedirectory
  • Exclude files?
    2 Posts | Last post June 26, 2015
    • Tord,
      I see you mention in a response to Betsy below on excluding files but I haven't been able to get my script to do this. I'm using this script for multiple servers to clean up files such as SQL transaction log and full backup files older than # of days and I have other FTP folders where I need to delete all files except for certain index files used by web sites.
      Do I need to edit the deloldfiles.ps1 script to exclude files or is something added to the pathlist.txt file?
      Thanks for any assistance. I'm fairly new at PowerShell scripting so sorry if you actually answered this in your response to Betsy below as I am not understanding what needs to be changed.
      Thanks for any feedback,
    • There is a ton of ways to do it, if its just for a few files use an if/else statement, if not create a New file (whitelist.txt) or something, search the file to see if you can delete the file or not. :)
  • LastWriteTime
    2 Posts | Last post September 23, 2014
    • Thank you Brian that helped in my situation with copying of files to a different location.
      Thank you to all for this.
    • Anytime buddy, glad I could help!
  • delete folders
    2 Posts | Last post September 23, 2014
    • 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?
    • it shouldn't do that. did you rewrite something in the script?
  • Script runs but no files are deleted.
    3 Posts | Last post September 23, 2014
    • 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.
    • 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
    • A good night sleeps usually helps! :)
  • A slight modification of interest
    2 Posts | Last post September 23, 2014
    • 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
    • Nice one, thanks Brian!
  • Delete Folders?
    2 Posts | Last post September 23, 2014
    • 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...
    • All you need to do is "rewrite" it and remove my "-Include".. :)
1 - 10 of 14 Items