Assuming you don't have a list of sites or users to work from, just enumerate the directories under d:\userdata
, to get the sites, then the directories under that to get the path for each user.
$root = "d:\userdata";
$sites = Get-ChildItem -Path $root -directory;
foreach ($site in $sites) {
$siteusers = get-childitem -path $site.FullName -Directory;
foreach ($siteuser in $siteusers) {
$downloaddir = Join-Path -Path $siteuser.FullName -ChildPath "Downloads";
Get-ChildItem -Path $downloaddir -Recurse -File | Where-Object{$_.LastWriteTime -lt $(get-date).AddDays(-30)} | Remove-Item -force;
}
}
Obviously test this against non-production data first.
If you already have a list of sites (by directory name), it looks pretty much the same:
$sites = @("Site1","Site2");
$root = "d:\userdata";
foreach ($site in $sites) {
$SitePath = Join-Path -Path $root -ChildPath $site;
$siteusers = get-childitem -path $SitePath -Directory;
foreach ($siteuser in $siteusers) {
$downloaddir = Join-Path -Path $siteuser.FullName -ChildPath "Downloads";
Get-ChildItem -Path $downloaddir -Recurse -File | Where-Object{$_.LastWriteTime -lt $(get-date).AddDays(-30)} | Remove-Item -force;
}
}
You could then run multiple instances of this, each with a different list of site names in $sites
.
Another option there would be to collect the site names via get-childitem
(into an array/collection), then split it up to have one instance of the script process the even-numbered array elements, and another process the odd ones.