I'm trying to make a script on a users pc that will run a PowerShell script once a week and clear out the contents of a users download folder.
i have tried the following scripts but cant get them to work.. Im not to familiar with PowerShell, so forgive me if the answer is easy.
I have tried the following scripts; they will work with users temp folder, but when I try to change it to downloads folder.
$CutOffDate = (Get-Date).AddDays(-7)
$AllTemp = "$($Env:Downloads.Replace("$Env:Username",'*'))"
Get-ChildItem -path $AllTemp -Recurse |
Where-Object {$_.CreationTime -lt $CutOffDate} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
and
$CutOffDate = (Get-Date).AddDays(-7)
Get-ChildItem -Path "C:\Users\*\Downloads\*.*" | Where-Object { $_.CreationTime -lt $CutOffDate } | Remove-Item
The first script throws errors and the second one says permission denied when I use a basics user profile. However if I use admin profile it wants to delete downloads content from admin user and not basic user. I'm unsure what do to - can anyone help?
Get-ChildItem -Path "C:\Users\*\Downloads\*.*"
with the conditionalwhere-object
make it a schedule task that runs asSYSTEM
. The schedule it to run once a week. UseRemove-Item "C:\Users\*\Downloads\*" -Recurse -Force;
or some tweaked variation.