Sometimes when moving from one Windows computer to another with a removable drive or I upgrade an older windows system and reuse a partition from the old system, some files and folders may be inaccessible. Typically, Windows will pop up a window with the message “You need permission to perform this action” and further details of the user that can grant you these permissions. For instance trying to delete a directory will yield a similar window:
The steps to take full control of the files again and carry on with the action you were intended to do requires of course Administrator rights. I use this two-step process:
(a) First take ownership of the directory, and its contents (recursively), in question:
takeown /F <dir> /R /D y
(b) Despite being the owner, you also need to have permissions to do what you are intended to do. Best is to allow Administrators full control:
icacls <dir> /grant Administrators:F /T
Right-click > Expropriate > Rinse > Repeat
Here is, upfront, the batch file that creates the Expropriate entry in the context menu:
I like to have useful commands available in the context menu when right-clicking a directory. To turn the above two-step process into a right-click menu command, first create the context menu entry by executing from a Windows Command line:
reg add HKEY_CLASSES_ROOT\Directory\shell\Expropriate /ve /f /d "Expropriate dir"
Then for the command to be executed when you click on that entry, execute (all in one line):
reg add HKEY_CLASSES_ROOT\Directory\shell\Expropriate\command /ve /t REG_EXPAND_SZ /f /d "%%comspec%% /c takeown /F %%1 /R /D y && icacls %%1 /grant Administrators:F /T"
The command will be available whenever you right-click a directory from within Windows Explorer, as shown in the figure below.
You can also download the batch script expropriate-dir-cmd.bat and run it (at your own risk) to create the relevant keys in your registry for the Expropriate command.