Remove old files from directories [Update - BugFix]

The script Remove-Files is used to remove old and/or obsolete files from the target directory. It can delete files that are older than a specified amount of time or older than a specified date or it can delete the oldest files in the directory keeping any number of recent files

Remove-Files.ps1
 
 
 
 
 
5 Star
(1)
3,475 times
Add to favorites
Storage
4/3/2012
E-mail Twitter del.icio.us Digg Facebook
  • Can your script also delete folders?
    2 Posts | Last post July 17, 2013
    • Just wondering if there's a way for your script to delete not only the old files but also any old folders.  Great script by the way.
    • Actually the script just deletes files.
      In this line
      $strGetChild = "Get-ChildItem -Path `"$Path`" $RecOption $ExtOption | Where-Object {`$_ -is [System.IO.FileInfo]}"
      it just sselects the files that get-childitem returns.
      So you could remove the where statement to start playing around and making it delete folders to. 
      Since I had no need to delete folders I never tested this.
      If I find some time next week, I'll test it and maybe post an update.
      However if you like the script so far, how about rating it ;-)
      
      Regards
      Marcus 
  • How to make as a module
    2 Posts | Last post April 26, 2013
    • I really like this and would love to make it into a module that I can use elsewhere.  The initial parameters section seems to prevent some of this.  As a newbie, I'm not sure what it would take to move this code into a module.
    • Converting the script into a module is straight forward. All you have to do is wrap the whole script in a function called Remove-Files and save the script as remove-files.psm1 in your modules Directory.
      
      Step by step this would be:
      1. Open the script
      2. Add this as new first line:
         function Remove-Files{
      3. Add closing curly braket at the end of the script.
      4. Create a folder Remove-Files under $home\Documents\WindowsPowerShell\Modules
      5. Save the changed file as Remove-Files.psm1 in this folder
      6. You're done!
      
      HTH
      Marcus 
  • LiteralPath
    2 Posts | Last post January 24, 2012
    • When running this, I get "Cannot bind argument to parameter 'LiteralPath' because it is null"
      How do I resolve this?
    • Can you give me the full error please?
      Or the line number the error comes from.
      And do you run it from a Powershell host or are you running it from within an editor like PowerGUI?