• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
ControlUp Community

ControlUp Community

Connect, Learn, and Grow

  • Blog
  • Archives
  • Findings
  • Meetups
  • Videos
  • Events
  • Categories
    • ControlUp One Platform
    • ControlUp for Apps
    • ControlUp for Compliance
    • ControlUp Dashboards
    • ControlUp for Desktops
    • ControlUp for VDI
    • ControlUp Scripts & Triggers
    • ControlUp Synthetic Monitoring
    • ControlUp Workflows
  • Topics
    • Logos & Wallpaper
    • ControlUp.com
  • Join

Hwo to Search a File Share and Delete the ‘Downloads’ Folder Contents with ControlUp Scripts

Posted on January 10, 2023

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
Topics: Microsoft Windows, Scripts

Ask Us Anything, Connect, Learn, and Grow with the ControlUp Community!

Login to the ControlUp Community to ask us anything, stay up-to-date on what’s new and coming soon and meet other like-minded techies like you.

Not already a member? Join Today!

Primary Sidebar

ControlUp Academy

Enroll in ControlUp Academy for expert-led technical training, equipping you with skills to effectively deploy, manage, and grow your ControlUp investment.

Learn here >

Rotating Images

Hidden Gem from our Community on Slack!

ControlUp Betas - What's Coming Next?
NEW ControlUp Features - Stay Up-to-Date!
ControlUp Scripts - Scripting, Zero to Hero
Latest KB Articles - Be the First to Learn

Video Tutorials Library

Visit our technical how-to videos, offering step-by-step tutorials on advanced features, troubleshooting, and best practices.

Watch here >

ControlUp Blog

Check out the ControlUp blog for expert advice and in-depth analysis.

Read here >

ControlUp Script Library

Visit the ControlUp technical script library, which offers a multitude of pre-built scripts and custom actions for your monitoring and troubleshooting requirements.

See here >

ControlUp Support

Visit the ControlUp support home and to delve deeper into ControlUp DEX solutions.

Browse here >

Footer

      

ControlUp Community
Of Techie, By Techie, For Techie!

Terms of Use | Privacy Policy | Security
Dive Deeper, Learn more at ControlUp.com

  • facebook
  • twitter
  • youtube
  • linkedin

© 2023–2026 ControlUp Technologies LTD, All Rights Reserved.

We use cookies to ensure that we give you the best experience on our website. by continuing to use this site you agree to our Cookie policy..