Reset file ownership based off parent folder name

For anyone who does quota management, I’m sure you’ve run into issues where the Administrator account is incorrectly the owner of 1,000,000 files located in various users’ folders. Of course, you can go to each user’s folder and manually reset it to the correct owner. But, why?

If your user folders are set up in this format:

D:\Users\<username>

You can simply use FOR loop along with the ICACLS command to reset the ownership of all of the files (among other things).

Here’s the snippet:

@echo off
SET D= D:\users
 
FOR /F %%A IN ('DIR %D% /AD /B') DO icacls %D%\%%A /setowner %%A /t /c /q

Just put it in a .bat file and you’re good to go!

For more information, check out the documentation on ICACLS at
http://technet.microsoft.com/en-us/library/cc753525(WS.10).aspx.