Clean up script for subfolder in users

[int]$retentionDays = 90
$folders = "downloads", "OneDrive\Microsoft Teams-chatfiles"

$filesForDeletion = @()
$users = (Get-ChildItem "c:\users").name

foreach ($user in $users) {
foreach ($folder in $folders) {

$path = "C:\users\"+$user+"\"+$folder
if (Test-Path $path) {
$files = Get-ChildItem $path

foreach ($file in $files) {

if ($file.CreationTime -lt (Get-Date).AddDays(-$retentionDays)) {
$filesForDeletion += $file
}}}}}

rm $filesForDeletion.fullname -Confirm:$false -Force -Recurse

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.