Permission denied deleting user directories and files Windows SBS

I came across an issue removing user’s directories and files from Windows Small Business Server (SBS) 2011. Even though I was using my account that is in the Domain Administrators group that is an administrator on the server,.

I could delete some of the directories and files but not all of them and was met with a “Folder Access Denied”  advising that “you required permission from the computer’s administrator to make changes to this folder”.

Folder Access Denied. You need permission to perform this action.

Folder Access Denied

On some directories going to the Owner tab and manually selecting a new owner then deleting worked, but this is quite a slow and laborious process and didn’t work for all directories.

In the example below I’ll demonstrate how I overcame this using the command line. This example is for a user in the D:\Users\FolderRedirections directory (your user files may be located in a different area).

Open a command prompt as an administrator.

Navigate to the directory by using the “change directory” command.

D: cd Users\FolderRedirection\username

change directory to FolderRedirection

 

Use the “takeown” command to take ownership of the directories and folders (in this example I show it just for the Downloads directory).

The /F switch indicates the directory

/A gives ownership to the administrators group

/R recurse

/D Y gives the default answer of Y to take take ownership

(Use takeown /? to see full explanation)

takeown

I tried to delete the folders again but was greeted with the same prompt. Viewing the permissions I could see the administrators group had full permission but it still wouldn’t let me delete the files.

Using the command line again to run icacls.exe program command.

icacls-exe

processed file: Downloads Successfully processed 1 files; Failed processing 0 files

icacls result

Note, if you are doing this in PowerShell then you’ll have to hold the icacls parameters in a variable and pass it the the command.

 

Finally I could remove the directories.

I made this into a PowerShell script that I run to remove old user’s directories and files easily which I will post soon.

Note: on one occasion this didn’t work and after running the above process I had to go into the properties of the directory by right clicking and selecting properties and add my user account to full access (even though it was already in there).