Taking ownership of files can be a very tedious task. If you select more than one file, the Security tab is not available. This means that you have to right-click on each individual file, go to Properties, select Security, select Advanced, and take ownership of the file. If you’re working with any significant number of files, that is far too time consuming. Once you take ownership of the files, you also want to change the permissions on the file to give yourself access.
I found myself in this situation this weekend. There was a folder full of files but I needed to delete only the ones that started with the word ‘pending’. I did not have permission to access the files as they’re controlled by SYSTEM.
Opening up the elevated command prompt, I ran this command:
dir path\to\folder\pending*
This listed all of the files in the folder that started with ‘pending’. I confirmed that the list exactly matched the files I needed to delete. I then ran the command
takeown /f path\to\folder\pending*
This successfully gave me ownership of each of those files. Now I could go modify the permissions, but since I already have Command Prompt open…
cacls path\to\folder\pending* /e /p [username]:[permission]
This gave me the permission that I specify at the very end since I specified my user name. The available permission options are:
- N – none
- R – Read
- W – Write
- C – Change (write)
- F – Full control
The /e specifies to edit the ACL instead of replacing it and the P specifies the new permission. Since I was deleting the files, I gave myself full control over the files. So the actual command looked like:
cacls pending* /e /p Jason:F
cacls.exe is actually deprecated, so I should have used icacls to change the permissions.
icacls folder\pending* /grant [username]:(F)
Icacls allows for a lot more complexity when specifying permissions. Your options or simple rights are:
- N – no access
- F – full access
- M – modify access
- RX – read and execute access
- R – read-only access
- W – write-only access
- D – delete access
You can also specify a comma-separated list of specific rights instead. Use icacls.exe /? to see your full list of options.