Let’s say you have some user folders or some other such folder (or file) in Windows that you need to delete, but you get an unauthorized access error.

There are a couple of things you need to do before you can remove a protected folder/file. This process assumes you are part of the Administrators group. Also, run the command prompt as admin when running these commands.

Take ownership
Grant permissions
Remove the folder

Take Ownership

This command takes ownership recursively of an entire folder and its contents.

takeown /r /d Y /f C:\DELETEME

Grant Permissions

This command grants permissions recursively for an entire folder and its contents to anyone in the Administrators group.

icacls C:\DELETEME\* /grant Administrators:F /t /c

Add the /q argument to the end to run the command in quiet mode.

icacls C:\DELETEME\* /grant Administrators:F /t /c /q

Remove the Folder

This command deletes a folder and all of its contents (recursive).

RMDIR C:\DELETEME /S

Add the /q argument to the end to run the command in quiet mode.

RMDIR C:\DELETEME /S /q

Remember, you can really break some stuff if you go poking around deleting system files and whatnot. So, ask yourself if you should be deleting a protected file before you go ahead and press enter. I came across this process when trying to delete orphaned user folders. So, if you’re trying to delete user profiles too, please be aware that there is more than just a user folder involved with a Windows profile. There are some registry locations as well.