Delete files from Sub-Folders of users older than x Days

Welcome Forums General PowerShell Q&A Delete files from Sub-Folders of users older than x Days

Viewing 5 reply threads
  • Author
    Posts
    • #203009
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      I have a primary folder c:\Directory under that is a %username% directory and under that is a Downloads directory with multiple directories in that folder. I need to create a script that will delete files older than 7 days from the Downloads directory of every user in the in the primary C:\directory\%username%\Downloads …. and logs the files that have been downloaded.

       

       

    • #203012
      Participant
      Topics: 12
      Replies: 1642
      Points: 2,660
      Helping Hand
      Rank: Community Hero

      What have you tried? Have you looked at Get-ChildItem?

    • #203018
      Participant
      Topics: 0
      Replies: 3
      Points: 15
      Rank: Member
      $dateFilter = (Get-Date).AddDays(-7)
      
      $filesToRemove = Get-ChildItem -Path 'SomeUserDirectory' -File -Recurse | Where-Object {$_.LastWriteTime -lt $dateFilter }
      
      $filesToRemove | Foreach-Object {$null = Remove-Item -Path $_.FullName -Force}

      This isn’t tested. Test it.

      • This reply was modified 4 months, 3 weeks ago by grokkit. Reason: removed double post and fixed code formatting
    • #203024
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      I have tried Get-ChildItem but that will only get me down one level from the c:\directory. I need to go all the way down to the Downloads directory in every users folder to do the delete.

    • #203027
      Participant
      Topics: 0
      Replies: 3
      Points: 15
      Rank: Member

      I have tried Get-ChildItem but that will only get me down one level from the c:\directory. I need to go all the way down to the Downloads directory in every users folder to do the delete.

      # This still isn't tested, please test before using. It should meet all of your requirements.
      
      $dateFilter = (Get-Date).AddDays(-7)
      
      $userDownloadFolders = Get-ChildItem -Path C:\Users\ -Recurse -Directory -ErrorAction SilentlyContinue | ? {$_.Name -eq 'Downloads'}
      
      $filesToRemove = @()
      
      foreach ($userDownloadFolder in $userDownloadFolders )
      {
          $filesToRemove += Get-ChildItem -Path $userDownloadFolder.FullName -File -Recurse | ? {$_.LastWriteTime -lt $dateFilter}
      }
      
      $filesToRemove | % {$null = Remove-Item -Path $_.FullName -Force}
      • This reply was modified 4 months, 3 weeks ago by bishoparp40.
      • This reply was modified 4 months, 3 weeks ago by bishoparp40.
    • #203030
      Participant
      Topics: 5
      Replies: 2367
      Points: 5,981
      Helping Hand
      Rank: Community MVP

      # This still isn’t tested, please test before using. It should meet all of your requirements.

      Could you please format your code as code using the code tag button (“PRE”)? Thanks.

      You may re-read the very first pinned post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!

Viewing 5 reply threads
  • The topic ‘Delete files from Sub-Folders of users older than x Days’ is closed to new replies.