Announcement

Collapse
No announcement yet.

Script to remove old files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to remove old files

    I'm looking for help creating a PowerShell script to remove files older than 60 days from multiple folders while omitting two particular files from this rule. The first 11 characters for all the files are the same, but the ones that I want to maintain end with a YTD while the others are Daily_xxxxx. The script to delete the files is no problem, but I'm running into an issue getting it to omit the particular files that I want to remain.. Any help would be greatly appreciated.

  • #2
    Will this do what you want to accomplish?
    Code:
    $limit = (Get-Date).AddDays(-60)
    Get-ChildItem 'D:\folder' -recurse | Where-Object {!$_.PSIsContainer -and $_.CreationTime -lt $limit -and $_.BaseName -notMatch ".*YTD$" } | Remove-Item -force  -whatif

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Rems, worked like a charm. Thanks for the help.

      Comment

      Working...
      X