Recipe 4.17. Taking Ownership of a File or FolderProblemYou 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. SolutionUsing a graphical user interface
Using a command-line interfaceUse 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 DiscussionIf 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 AlsoMS 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) |