4.17. Taking Ownership of a File or Folder

Problem

You want to take ownership of a file or folder. This may be necessary if you find that NTFS permissions have you locked out of a file or folder. As long as you are an administrator of the system, you should be able to take control of it and reset permissions as necessary.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. In the left pane, browse to the parent folder of the file or folder you want to take ownership of. Click on the parent folder. This displays the list of subfolders and files in the right pane.

  3. In the right pane, right-click on the target file or folder and select Properties.

  4. Select the Security tab.

  5. Click the Advanced button.

  6. Select the Owner tab.

  7. Under the Change owner to heading select the new owner and click Apply.

Using a command-line interface

Use the following command to attempt to take ownership of a file:

> takeown <FileName>

For example:

> takeown d:\iwanna.exe

If you want to grant ownership to someone else, use the subinacl.exe command:

> subinacl /file <FilePath> /setowner=<User>

For example:

> subinacl /file \\rallen-svr1\docs\guide.doc /setowner=AMER\rallen

Or you can even grant ownership to a user over all the files in a directory:

> subinacl /subdirectories \\rallen-svr1\docs\* /setowner=AMER\rallen

Using VBScript

' This code transfers ownership
 of the specified file to the
' user running the script.  If strFile is set to a folder path
' then ownership of all files within the folder will be changed. 
' ------ SCRIPT CONFIGURATION ------
strFile = "<FilePath>"       ' e.g., d:\scripts
strComputer = "<ServerName>" ' e.g., rallen-svr1 or . for local server
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objFile = objWMI.Get("CIM_DataFile.Name='" & strFile & "'")
intRC = objFile.TakeOwnership
if intRC = 0 then
   WScript.Echo "File ownership successfully changed"
else
   WScript.Echo "Error transferring file ownership: " & intRC
end if

Discussion

If you are taking ownership of a file or folder because you were locked out of it, even after you take ownership you still have to go in and grant yourself the necessary NTFS permissions to access and manipulate the file or folder.

With Windows Explorer on Windows 2000, you can only assign one of the members of the local administrators group as an owner of a file or folder. With Windows Server 2003, there is a new button called Other Users or Groups that lets you use the object picker to select any user as an owner.

See Also

MS KB 268019 (HOW TO: Take Ownership of Files), and MS KB 320046 (HOW TO: Use the File Ownership Script Tool (Fileowners.pl) in Windows 2000)

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.