

' Registry tutorial - how to take ownership of folders and files by Andy.

' To add Take Ownership just run the program as it is.
' To remove Take Ownership comment out TakeOwnership()
' and uncomment RemoveFromContextMenu(). 

' Always build first, then find the exe file and run as admin.
' ------ ----- ------ ---- ---- --- --- ---- --- --- -- ------

$include "Registry.inc"

int result
istring Entry[1024] = "" 

openconsole
print
print

'The TakeOwnership routine adds Take ownership to folder & files context menu.
TakeOwnership()

'The RemoveFromContextMenu routine removes Take ownership from the folder & files context menu.
'RemoveFromContextMenu()

print
print
do:until inkey$ <> ""
closeconsole
end

sub TakeOwnership()

'Add to registry Take Ownership context menu option.

'For Files.
Entry = "cmd.exe /c takeown /f " + chr$(34) + "%1" + chr$(34) + " && icacls " + chr$(34) + "%1" + chr$(34) + " /grant administrators:F"

result = RegSetValue("HKEY_CLASSES_ROOT\*\shell\runas","Take Ownership","")
result = RegSetValue("HKEY_CLASSES_ROOT\*\shell\runas","","NoWorkingDirectory")
result = RegSetValue("HKEY_CLASSES_ROOT\*\shell\runas\command",Entry,"")
result = RegSetValue("HKEY_CLASSES_ROOT\*\shell\runas\command",Entry,"IsolatedCommand")

'For Folders.
Entry = "cmd.exe /c takeown /f " + chr$(34) + "%1" + chr$(34) + " /r /d y && icacls " + chr$(34) + "%1" + chr$(34) + " /grant administrators:F /t"

result = RegSetValue("HKEY_CLASSES_ROOT\Directory\shell\runas","Take Ownership","")
result = RegSetValue("HKEY_CLASSES_ROOT\Directory\shell\runas","","NoWorkingDirectory")
result = RegSetValue("HKEY_CLASSES_ROOT\Directory\shell\runas\command",Entry,"")
result = RegSetValue("HKEY_CLASSES_ROOT\Directory\shell\runas\command",Entry,"IsolatedCommand")

print "  Take Ownership has been added to the context menu."

return 0
endsub

sub RemoveFromContextMenu()

'Remove from the registry
result = RegDeleteKey("HKEY_CLASSES_ROOT\*\shell\runas\command")
result = RegDeleteKey("HKEY_CLASSES_ROOT\*\shell\runas")
result = RegDeleteKey("HKEY_CLASSES_ROOT\Directory\shell\runas\command")
result = RegDeleteKey("HKEY_CLASSES_ROOT\Directory\shell\runas")

print "  Take Ownership has been removed from the context menu."

return 0
endsub

