quiet_andy Posted July 20, 2017 Posted July 20, 2017 I'm trying to find a script which when ran will delete the contents of each users downloads folder. The specific would look like: D:\User Folders\Students\Admitted 2016\user1\Downloads D:\User Folders\Students\Admitted 2016\user2\Downloads But the user folders are actually their names. Anyone have an idea or know of something which will work?
Rob_D Posted July 20, 2017 Posted July 20, 2017 (edited) This is based off a script we made to clear out user folders for sets of users. It should do what you want but make sure you test it first. I take no responsibility if it deletes everything in your userdata folders or something. pushd [color=#333333]D:\User Folders\Students\Admitted 2016 [/color] if exist C:\Scripts\Logs\Empty_Exam_UserData.log del C:\Scripts\Logs\Empty_Exam_UserData.log if not exist .\Downloads md .\Downloads for /D %%f in (*\Downloads) do robocopy /mir .\Downloads ".\%%f" >>C:\Scripts\Logs\Empty_Exam_UserData.log Edited July 20, 2017 by Rob_D 1
quiet_andy Posted August 9, 2017 Author Posted August 9, 2017 I've just given the script a test run and all it does is create a downloads folder in d:\user folders\students\admitted 2016 and does not go into each sub folder. Any ideas?
mukz Posted August 9, 2017 Posted August 9, 2017 Hey Andy, i made one to do exactly what you wanted at my last school. Didnt take the script with me but will ask the guys over there to send me it across. 1
Rob_D Posted August 9, 2017 Posted August 9, 2017 That's odd, I can't see why it shouldn't be copying the folder over. Try this, it might take a little longer but should be more robust. pushd D:\User Folders\Students\Admitted 2016 for /D %%f in (*) do rmdir /s /q ".\%%f\Downloads" for /D %%f in (*) do mkdir ".\%%f\Downloads"
quiet_andy Posted August 9, 2017 Author Posted August 9, 2017 (edited) I've managed to cobble together this, it appears to work on my test directory. Will be trying it very soon. If anyone spots any glaring mistakes, please let me know. $rootpath="e:\test" $dirs = Get-ChildItem -Path $rootpath | Where-Object { $_.Attributes -eq "Directory" } Foreach ($dir in $dirs) { $downloadpath = $files + "\" + $dir + "\Downloads\*" Remove-Item -Recurse -Force $downloadpath } This finds each user folder and creates the variable to their downloads folder, then removes all the files and sub folders without removing the downloads folder. Edited August 9, 2017 by quiet_andy
Steve21 Posted August 9, 2017 Posted August 9, 2017 I'm guessing it's due to random space savings but I'd be careful deleting those as don't forget things like email attachments etc auto get saved to downloads if no one picked a specific folder, and if people do like here and email work in then save it there you could be deleting a lot of work Steve
quiet_andy Posted August 9, 2017 Author Posted August 9, 2017 My plan is to teach them all a lesson on saving work, we have found some users with 4gb in their downloads.
buzzard Posted November 14, 2017 Posted November 14, 2017 I had a script that did exactly this but only on files over X days old. I've managed to delete my script and came hunting for something! Thanks for this
HPlum78 Posted November 15, 2017 Posted November 15, 2017 $UserNames = get-aduser -SearchBase "OU=Users,OU=StaffAccounts,DC=SCHOOL,DC=COM" -Filter * | Select samAccountName $UserNames | %{ $FldrPath = "D:\User Folders\Students\Admitted 2016\$($_)\Downloads" Set-Location $FldrPath | Remove-Item * -Recurse } The above is one approach that you could use.... I would throw in a test-path and again some logging.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now