# This script checks each file and folder for its last modify date # script checks each file and folder if it is older then 10 days and removes it) $Path = "D:\Shared Folder" $Period = (Get-Date).AddDays(-10) $ItemsForRemoval = @() $fileRemovalMessage = "Removing file {0} from {1}" $folderRemovalMessage = "Removing directory {0} from {1}" # Getting all users folders. $usersFolders = Get-ChildItem -Path $path Foreach ($userFolder in $usersFolders) { # Accessing each folder and getting from there the items # that matching the if statement. Get-ChildItem -Path $userFolder.FullName -Recurse | ForEach-Object { # You can change or extend the condition here. if ($_.CreationTime -lt $period) { $itemsForRemoval += $_ } } } # The actual removal block. $itemsForRemoval | Sort-Object -Property {$_.GetType()} -Descending | ForEach-Object { $itemName = $_.Name $itemPath = $_.FullName $itemParent = $_.Parent.FullName if ($_.GetType().Name -eq 'DirectoryInfo') { if (Get-ChildItem -Path $itemPath) { Write-Host ($folderRemovalMessage -f $itemName,$itemParent) Remove-Item -Path $itemPath -Force -confirm:$false } } else { Write-Host ($fileRemovalMessage -f $itemName,$itemParent) Remove-Item -Path $itemPath -Force -confirm:$false } }