Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I found this command here.

The command is structured as a registry modification that will add a "Take Ownership" entry in the standard Windows Explorer file/directory context menu. I can see where it uses cmd.exe, and I can sort of understand what's happening from there.

The registry modification contains several commands, but I'm specifically interested in the command to take ownership and grant permissions on a directory tree:

[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Grant Admin Full Control"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"
"IsolatedCommand"="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"

One of the problems is that - because of the way the command is executed - it explicitly invokes cmd.exe with the /C switch. This causes escape sequences to be required for quotation marks (which I don't entirely understand). The command also contains tokens that I have no idea how to use from the command-line ("takeown", "icacls", etc). On top of that, the command is split onto two separate lines, with a line-break in between - which I assume would cause cmd.exe to attempt to execute it as two separate commands, when it does not appear to be such.

I need to convert command into a script file (one that doesn't open another instance of cmd.exe - presumably a batch), with an explicit directory replacing the "%1". The only restriction at this point is that this script needs to be able to run as a shutdown script in the local Group Policy. I don't trust myself to do it correctly, since I have little understanding of the syntax used in this registry modification. As such, it's pretty risky for me to try to do it myself. Who knows what damage I could cause if I got the syntax wrong.

Therefore I'm asking if anyone can point me in the right direction - bonus points for explicit examples.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

takown and isacls are DOS commands. As there is no path specified to reach them in the Registry file, I assume they are available on the path.

Just create a batch file (say ownandgrant.bat) containing

@echo off
takeown /f %1 /r /d y 
icacls %1 /grant administrators:F /t

This could cause issues if the directory name contains 1 or more spaces so make sure you enclose the directory with double quotes when calling the batch file.

ownandgrant.bat "my special directory"
share|improve this answer
    
Your solution works like a charm. Thanks! –  Giffyguy Sep 9 '09 at 11:22
    
BTW if you want to call ownandgrant.bat (or whatever you named it) from another batch file, don't forget to use "call ownandgrant.bat". If you don't, it won't go back to your script after running ownandgrant.bat. –  Snark Sep 10 '09 at 9:39
    
The original uses && between the commands -- maybe just to get them on one "cmd /c" line -- but the side effect is that the icacls only runs if the takeown successfully completes. This may be important. –  quack quixote Oct 2 '09 at 1:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.