Change Ownership of a File or Folder Using Takeown Command-Line Tool

Every file or folder in a NTFS volume has a owner. Certain system files are owned by TrustedInstaller, some by SYSTEM account and others by the “Administrators” group. If a user creates a file or folder, that user is usually the owner of the file or folder. The owner is the one who can assign permissions (Allow or Deny) to other users for that object.

If a user is not the owner of a file or folder, or the user has no permissions to access the file, he gets the “Access is Denied” error when accessing the object. If that user is an administrator, he can take ownership of the object using the file or folder’s Properties – Security tab, and assign himself the required permissions.

This post tells you how to take ownership of a file or folder, and assign required permissions for it using the command-line instead of GUI.

Windows includes a command-line tool named Takeown.exe which can be used from an Admin Command Prompt to change the ownership of a file or folder quickly. Here is how to take ownership of a file or folder and then assign permissions for an account using command-line.

Take Ownership using command-line takeown.exe

Taking ownership of a file

Open an elevated Command Prompt window. Use the following syntax to take ownership of a file:

TAKEOWN /F <filename>

Replace <filename> with the actual file name with full path.

If the operation was successful, you should see the following message:

"SUCCESS: The file (or folder): "filename" now owned by user "Computer Name\User name"."

Assign Permissions

Then to grant Administrators Full Control permissions for the file, use ICACLS. Here is the syntax:

ICACLS <filename> /grant administrators:F

Another example: To assign Full Control permissions for the currently logged on user, use this command:



ICACLS <filename> /grant %username%:F

%username% represents the account name of the currently logged on user. ICacls accepts this variable directly.

Taking ownership of a folder

Use the following syntax:

takeown /f <foldername> /r /d y

Assign Permissions

Then to assign the Administrators group Full Control Permissions for the folder, use this syntax:

icacls <foldername> /grant administrators:F /T

The /T parameter is added so that the operation is carried out through all the sub-directories and files within that folder.

Command-line help:

To know the complete usage information for Takeown.exe and ICacls.exe, run these commands from a Command Prompt window.

takeown /?
icacls /?

Easier Methods for Taking Ownership

Command Script

To further simplify the process of taking ownership, Tim Sneath of Microsoft provides a .CMD file (Windows Command Script) which takes ownership and assigns Full Control Permissions to Administrators for the directory which is passed as a parameter to the CMD file. For more information, read Tim’s post Secret #11: Deleting the Undeletable.

Add "Take Ownership" command to the Context menu

This again uses the special runas verb in Windows Vista and higher, which I’ve covered earlier (REF RunAs).

via WinMatrix.com

Download takeown_context.reg and save to Desktop. Right-click on the file and choose Merge. Click Yes when asked for confirmation. This adds an extended command named Take Ownership in the context menu for files and directories. To access the command, you need to press and hold the SHIFT key, and then right-click on a file or folder.

About the author

Ramesh Srinivasan is passionate about Microsoft technologies and has a vast experience in the ITeS industry — delivering support for Microsoft's consumer products. He has been a Microsoft Most Valuable Professional [MVP], for 10 years in a row from 2003 to 2012 for his contribution in various Windows support forums.

27 thoughts on “Change Ownership of a File or Folder Using Takeown Command-Line Tool

  1. Well, it seems very helpful, but for some reason it keeps saying “Access is denied” not sure how to get around this.

  2. Thank you so much! You’ve just help me to recover some files off my laptop’s fracked old hard drive! :)

  3. Please could any one let me know the command to restore the owner ship to “Trusted installer” as a default owner. steps i performed is :

    1- TAKEOWN /R /F “C:\Program files” (SUCCESS & now i could see owner is SYSTEM)
    2 – cacls “c:\Program files” /T /E /G ProgFiles:W (ProgFiles is the local group) – SUCCESS

    Now here i tried below to restore the ownership:
    icacls “C:\Program files” /setowner “NT SERVICE\TrustedInstaller” /t /c
    ( It gave me message saying , successfully processed 897 files, failed processing 1134 files. (and still i could see SYSTEM as owner and not Trusted installed). PLEASE HELP ME

  4. This has finally allowed me to delete Windows and other system files from my slave drive. Even after inputting all those commands myself by following other peoples’ guides and getting ownership, it would still ask for permission infinitely and never get deleted. I assume perhaps it’s the runas verb that did the trick (none of the other sites ever mentioned it.) Either way, thank you so much for this. I was about to resort to formatting the whole drive and go through tedious data transfers.

  5. I had converted to Windows 8 from 7. I backed up my files from Windows 8 manually. I then reinstalled Windows 8 (because I hated it), then when I went to copy files back from the backup drive, I couldn’t because I did not have access. Your info helped me straighten out ownership and I can access them normally. Thanks.

  6. Thanks so much! As part of recycling an old XP computer, I had backed it up with Acronis. I had then “wiped” the old XP drive. When I restored specific pictures from the old XP machine backup to a W7 attached USB hard drive, I did not have access. I thought I might have to resurrect the old XP machine. But the above “saved my bacon”!

  7. this is great and it work (Take Ownership), I was able to replace the bad DLL in SysWoW64.

    What would be the reverse command for giving bacjk ownership? I would like to right-click on the directory and re-apply the same settings as before. For security reason.

    thanks

  8. THIS WAS PERFECT FOR ME!! >>WINDOWS 7 64 BIT<<

    ——————TO INSTALL—————
    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\*\shell\runas]
    @="Take Ownership"
    "NoWorkingDirectory"=""

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

    [HKEY_CLASSES_ROOT\Directory\shell\runas]
    @="Take Ownership"
    "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"

    —————TO UNINSTALL—————

    Windows Registry Editor Version 5.00

    [-HKEY_CLASSES_ROOT\*\shell\runas]

    [-HKEY_CLASSES_ROOT\Directory\shell\runas]

  9. This seemd to work but I still get access denied on our SBS 2008 for some folders. I don’t know if this is because I am working over a remote link?

  10. Thanks Ramesh!
    You just saved me from a very newbie user who screwed up things badly, this was part of the solution to get things back on track :)

  11. Needed access to pictures on hdd from dead laptop, connected via usb/hdd. Tried the above, but couldn’t get access, until I added /a to the command.

    Great tip otherwise

  12. Thanks mate… worked perfect…
    just a note to everyone else… u dont need to assign to Admin, it can be any username

  13. If you took ownership and still can’t do anything to the file.. it is because you don’t have any permissions on it yet. It sounds stupid.. but you need to take ownership and also grant permissions using the ICACLS functions I used this method to recover files on a HDD from a crashed PC that had some corrupted OS files and thus was not bootable, this method was a life saver as setting permissions via mouse click forms was a huge pain in the ass spread across 1000 some files. I even had issues with permissions not rolling down the directories making me have to click on each file to change permissions.
    so thanks for posting this

  14. Stupid question, but does the command-line method for Win7 grant permanent access to the file/folder? Or will that ownership be lost upon restart? Just curious, and also if that ownership can be reversed via similar syntax?

    -Thanks in advance

  15. I have at least one file for which this doesn’t work.
    “Take ownership” pretends that all is OK, but it is not possible to rename or delete file.

  16. Maybe it was a German, who developed this tool:
    If you use /f folder /r /d j (for german: JA), then there`s no error!
    At least in my german win7 it works … ;-)

Comments are closed.

%d bloggers like this: