Carrot63 Posted May 12, 2015 Posted May 12, 2015 Hi Please could anyone offer advice as to the quickest and most reliable way to delete a particular named folder and all of its subfolders and files from within all students home areas? The folder would effectively be "Users\Year12\fredbloggs\Library" and all that is below it. Thanks Carolyn
Chuckster Posted May 12, 2015 Posted May 12, 2015 You could pop this into a .BAT file as a login script... @echo off del /f /s /q "H:\Library\*" This will effectively delete all files and any sub folders within the Library folder. You could use GPP which is probably much easier.
Carrot63 Posted May 12, 2015 Author Posted May 12, 2015 Thanks. I would ideally like to have it as a script that I can run as a one off this evening but then retain in case I need it again. Therefore it would need to be able to automatically pick up all of the individual usernames in the Year12 folder. There are also a number of awkward files in each of them with long filenames etc..
halbaradkenafin Posted May 12, 2015 Posted May 12, 2015 Assuming you have all the pupil folders in the same location: $RootFolder = "\\server\path\to\root" $UsersFolders = Get-ChildItem -Path $RootFolder foreach ($Folder in $UsersFolders) { if (Test-Path "$Root\$Folder\rootpathtoremove") { Remove-Item -Path "$Root\$Folder\rootpathtoremove" -force -whatif } } Runs in Powershell, just change the paths for the root directory and the path within each users area. It will first test that the folder exists (to stop it throwing errors basically) and then tell you which folder it's removing, once you're happy that the correct folder is being removed from each user then just change -whatif to -confirm:$false and it will remove the folder and all child items. 1
Carrot63 Posted May 12, 2015 Author Posted May 12, 2015 That is absolutely fab thanks. I had to change the $Root\ to $RootFolder but it is now perfect for what I need. Thank you
halbaradkenafin Posted May 12, 2015 Posted May 12, 2015 That is absolutely fab thanks. I had to change the $Root\ to $RootFolder but it is now perfect for what I need. Thank you That was me forgetting to change it between testing it in a console and typing it up here, I should have just used Get-History | clip and just pasted that.
Carrot63 Posted May 12, 2015 Author Posted May 12, 2015 Sorry - one extra thing. The directories I am removing come up with a lot of access denied errors and directory not empty, presumably because of the access denied (even though I am admin). I think this has been caused because of a corruption caused by the link between the Macs and AD network, we think because dedupe is now on the servers. Do you know what extra switches we could add to take ownership/change permissions or force it to allow this to work?
halbaradkenafin Posted May 12, 2015 Posted May 12, 2015 Sorry - one extra thing. The directories I am removing come up with a lot of access denied errors and directory not empty, presumably because of the access denied (even though I am admin). I think this has been caused because of a corruption caused by the link between the Macs and AD network, we think because dedupe is now on the servers. Do you know what extra switches we could add to take ownership/change permissions or force it to allow this to work? The -force switch was already there which should handle that, the other option would be to run it on the server (as either a domain admin or local admin) it's hosted on and change the path to C:\ or D:\ (or whatever you use). If that doesn't work then it's possible to take control of those folders using a module called NTFS Security and the Set-Owner command.
Carrot63 Posted May 12, 2015 Author Posted May 12, 2015 Hi Thanks very much for your assistance. You showed me the way and after a bit of trial and error the following script deletes nearly all of the library folders in the students folders. The takeown struggles with a handful of files that are a real issue but it gets the majority. $RootFolder = "\\servername\studentshare\year12" $UsersFolders = Get-ChildItem -Path $RootFolder foreach ($Folder in $UsersFolders) { if (Test-Path "$RootFolder\$Folder\Library") { takeown /F "$RootFolder\$Folder\Library" /R /A /d Y cmd /C rmdir $RootFolder\$Folder\Library /s /q } } The rmdir at the end allows all of the the longfile names to also be deleted. Thanks 2
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