none
PowerShell: Script to delete files older than X days RRS feed

  • Question

  • Hi All,

    I am testing a script to delete files and I have it written down in my notepad as below:

    $foldername = $args[0]
    $maxage =      $args[1]

    gci $foldername | Where-object {($_-is [io.fileinfo]) -and ($_.lastwritetime -lt (get-date).AddDays(-$maxage) ) } |

    remove-item -whatif

    Question: When i try to run the script and  pass the parameters $foldername and 30 it doesn't return anything. I am expecting it to return with -Whatif

    To execute the script I wrote:

    .\myscipts\del.ps1  cmd2 30

    cmd2 is the name of the folder in my drive and 30 is ($maxage)  number of days old.

    Thank-you

    SQL 75

    Wednesday, March 4, 2015 10:38 PM

Answers

  • Start like this:

    Get-Childitem $foldername -File | Where-object {$_.lastwritetime -lt (get-date).AddDays(-$maxage)}


    ¯\_(ツ)_/¯

    • Marked as answer by SQL75 Wednesday, March 4, 2015 10:55 PM
    Wednesday, March 4, 2015 10:53 PM
  • Hi jrv, sorry but it still doesn't spit out the -whatif. Do you know why? I want it to display whatif clause when I run it. I added  "| remove-item -whatif" to your answer but puzzled why it doesn't work.

    Thank-you

    SQL 75

    • Marked as answer by SQL75 Wednesday, March 4, 2015 11:25 PM
    Wednesday, March 4, 2015 11:01 PM
  • No matching files.

    ¯\_(ツ)_/¯

    • Marked as answer by SQL75 Wednesday, March 4, 2015 11:25 PM
    Wednesday, March 4, 2015 11:07 PM

All replies

  • Start like this:

    Get-Childitem $foldername -File | Where-object {$_.lastwritetime -lt (get-date).AddDays(-$maxage)}


    ¯\_(ツ)_/¯

    • Marked as answer by SQL75 Wednesday, March 4, 2015 10:55 PM
    Wednesday, March 4, 2015 10:53 PM
  • Hi jrv, sorry but it still doesn't spit out the -whatif. Do you know why? I want it to display whatif clause when I run it. I added  "| remove-item -whatif" to your answer but puzzled why it doesn't work.

    Thank-you

    SQL 75

    • Marked as answer by SQL75 Wednesday, March 4, 2015 11:25 PM
    Wednesday, March 4, 2015 11:01 PM
  • No matching files.

    ¯\_(ツ)_/¯

    • Marked as answer by SQL75 Wednesday, March 4, 2015 11:25 PM
    Wednesday, March 4, 2015 11:07 PM
  • Thansks jrv, looks good! One more thing I wanted to ask do you know what it means by the error:

    "An Empty Pipe Element is not Allowed" I altered my script a bit but it gives me this error. and I am passing no parameters when i call the script. My Script I created is below:

    $name = $args[0]
    $chew = $args[1]

    IF ( $args.count -lt 2)  { Write-host "Please provide 2 parameters for this to work"
                    exit 1 }

    GCI $name | Where-object { $_-is [io.fileinfo]  -and ($_.lastwritetime -lt (get-date).AddDays(-$chew))}
     | remove-item -whatif

    Thank-you

    SQL 75

    Wednesday, March 4, 2015 11:29 PM
  • Sorry Jrv, I think I found the error. backtick was missing as I put in 2 lines.

    Regards

    SQL 75

    Thursday, March 5, 2015 12:03 AM