A user asked about existing scripts for deleting all contents in "Downloads" folders on file shares. ControlUp has a script for this (https://www.controlup.com/script-library-posts/clean-user-temp-folder/) and a helpful member provided a solution for a very quick and dirty solution, recursion for subfolders and error handling. Advanced training from ControlUp is recommended. There is a CTX Article but it does not offer defining file age.
Read the entire ‘Delete ‘Downloads’ Folder Contents with ControlUp Scripts’ thread below:
Anyone have an existing scrip that will do something like this: search a file share and delete all contents in "Downloads" folders. example I’m using is to goto user home folder redirection share and search for all items in everyones "Downloads" folders. Perhaps delete items that are past a certain age date.
we have this – https://www.controlup.com/script-library-posts/clean-user-temp-folder/
I guess you can modify it to meet your requirement(we also have this for reference – https://www.controlup.com/script-library-posts/clean-windows-system-drive/)I wrote you a very quick and dirty solution, as we say in the Netherlands ‘garantie tot aan de deur’ 🙂
Target folder has 2 files, one smaller than 2 kb, one larger. And there is a subfolder with the same two files. The switch -Recurse specifies if subfolders should also be cleaned. Note this only removes files, not empty folders.
Function Remove-DatStuffWeDoNotWantAnymore {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, HelpMessage = ‘Target folder to remove dat stuff from.’)]
[string]$TargetFolder,
[Parameter(Mandatory = $true, HelpMessage = ‘Minimum amount of days ago the file was LAST MODIFIED to be marked for deletion.’)]
[int]$DaysOld,
[Parameter(Mandatory = $true, HelpMessage = ‘Minimum size of the file in Kb to be marked for deletion.’)]
[int]$MinimumSizeKb,
[Parameter(Mandatory = $false, HelpMessage = ‘Specifies if recursion should be used to look for files in subfolders of the Targer folder.’)]
[switch]$Recurse = $false
)
# Get current date to compare
[datetime]$Before = (Get-Date).AddDays(-$DaysOld)
$FilesToBeRemoved = Get-ChildItem -Path $TargetFolder -Recurse:$Recurse | Where-Object { ($_.LastWriteTime -lt $Before) -and ($_.Length / 1KB -gt $MinimumSizeKb) }
$FilesToBeRemoved | Remove-Item -WhatIf
}Note the function currently has -WhatIf set on the Remove-Item so it can be tested. Ye’ll need to remove that for it to actually delete anything.And there is 0 error handling. No force delete. No warnings. No free cookies etc etc.
As with all scripts that remove/delete/wipe, Danger, Will Robinson! Use with caution!

In the advanced training we go over on how to edit and manipulate the scripts we currently have and clean windows system drive is one of them. Let me know if you are interested and I can see about inviting you to the next one if you want
My 50 cents is you would need to remove so much from that Clean Windows System Drive script, it is faster to just write something simple like my example. Advanced training from @member is always higly recomended though!
There is CTX Article for this but you cannot define file or folder age anywhere
You are all the best!
sound good hit me up.! thanks so much! You rock. Check Is in the mail. Np bud. Just remember to leave that -WhatIf in place till you are 100% sure it is targetting the right files 😂
Continue reading and comment on the thread ‘Hwo to Search a File Share and Delete the ‘Downloads’ Folder Contents with ControlUp Scripts’. Not a member? Join Here!
Categories: All Archives, ControlUp Scripts & Triggers
