Takeown is a utility that has been around for a while and is now standard in Windows Server 2003 and Vista. Takeown allows you to take ownership of files and folders from the command line, so you can script it!
Basic examples:
To take ownership of a file
takeown /f c:myfile
To take ownership of a folder
takeown /f c:myfolder /r
Syntax switches:
TAKEOWN [/S system [/U username [/P [password]]]] /F filename [/A] [/R [/D prompt]]
Description:
This tool allows an administrator to recover access to a file that was denied by re-assigning file ownership.
Parameter List:
/S system Specifies the remote system to connect to.
/U [domain]user Specifies the user context under which the command should execute.
/P [password] Specifies the password for the given user context. Prompts for input if omitted.
/F filename Specifies the filename or directory name pattern. Wildcard “*” can be used to specify the pattern. Allows sharenamefilename.
/A Gives ownership to the administrators group instead of the current user.
/R Recurse: instructs tool to operate on files in specified directory and all subdirectories.
/D prompt Default answer used when the current user does not have the “list folder” permission
on a directory. This occurs while operating recursively (/R) on sub-directories. Valid values “Y” to take ownership or “N” to skip.
/? Displays this help message.
NOTE: 1) If /A is not specified, file ownership will be given to the current logged on user.
2) Mixed patterns using “?” and “*” are not supported.
3) /D is used to suppress the confirmation prompt.
Examples:
TAKEOWN /?
TAKEOWN /F lostfile
TAKEOWN /F \systemsharelostfile /A
TAKEOWN /F directory /R /D N
TAKEOWN /F directory /R /A
TAKEOWN /F *
TAKEOWN /F C:WindowsSystem32acme.exe
TAKEOWN /F %windir%*.txt
TAKEOWN /S system /F MyShareAcme*.doc
TAKEOWN /S system /U user /F MySharefoo.dll
TAKEOWN /S system /U domainuser /P password /F sharefilename
TAKEOWN /S system /U user /P password /F DocReport.doc /A
TAKEOWN /S system /U user /P password /F Myshare*
TAKEOWN /S system /U user /P password /F HomeLogon /R
TAKEOWN /S system /U user /P password /F Mysharedirectory /R /A
Reference Sites:
Microsoft Technet
Windows IT Pro