Take Ownership and Full Control of Folders in Vista

18 December, 2008

Take Ownership and Full Control of Folders in Vista

Every now and then an automatic update fails and leaves a directory in the c: drive with a Guid for a name, along the lines of C:3baf1f42abd933d331f0d49f. The most annoying thing about these is that you can’t delete them, even when running as administrator. This is because the SYSTEM account owns them and no-one else has any permissions on them.

To manually delete them, you need to take ownership and then give yourself permission on every folder and file individually. This obviously would take forever, so a scripting solution is required. I finally came up with this solution:

1.    Run PowerShell as administrator.
2.    Navigate into the root of the folder to process.
3.    Take ownership of all the files and folders by running

gci -r | %{takeown /f $_.FullName}
This gets all files and folders below the current directory, then for each one runs takeown filename. Takeown is a utility which comes with Vista.

4.    Once you have ownership you need to run icacls on all the files to get permissions on them. icacls is the Vista and Server 2008 replacement for xcacls. The command to run is:

gci -r | %{icacls $_.FullName /grant:r username:F}
username is either you log on name on a stand-alone machine or domainaccountname in a domain.

5.    Once you have permissions you can then run del -r

I found most of this information from Christopher Atkins blog.



Richard Willis

Written by

Copyright © 2024 SalamanderSoft Limited