Jump to content

Powershell or Script to remove content of specific folder in each user area


Recommended Posts

Posted

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?

Posted (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 by Rob_D
  • Thanks 1
  • 3 weeks later...
Posted
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?
Posted
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.
  • Thanks 1
Posted

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"

Posted (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 by quiet_andy
Posted

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 :p

 

Steve

  • 3 months later...
Posted
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 :)
Posted

$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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...