In tip 3528, we used the command line to set ownership of a file system object.

To determine the ownership of a file system object, I have scripted Owner.bat.

The syntax for using Owner.bat is:

owner <Object>

where <Object> is the path to the file or folder. Owner.bat returns the owner in an environment variable named $owner.

Examples:

owner "%USERPROFILE%" would set $owner to the owner of the logged on user profile.

owner %Systemroot%\system32\dllcache would set $owner to builtin\administrators.

owner %logonserver%\NETLOGON\logon.bat would set $owner to the owner of the logon.bat file in the NETLOGON share of the authenticating domain controller.

Owner.bat contains:

@echo off
if \{%1\}==\{\} goto syntax
if not exist %1 goto syntax
setlocal ENABLEDELAYEDEXPANSION
set $owner=NONE
set file=%1
set file=%file:"=%
set line=N
call :dod "%file%">nul 2>&1
if "%line%" EQU "N" goto dof
set $owner=%line:~30%
set $owner=%$owner: .=%
for /f "Tokens=*" %%o in ('@echo %$owner%') do (
 set $owner=%%o
)
endlocal&set $owner=%$owner%
goto :EOF
:dod
for /f "Tokens=*" %%a in ('dir %1 /q /ad^|findstr /L /V /c:"&"^|findstr /L /c:"<DIR>"^|shellesc') do (
 if "!line!" EQU "N" set line=%%a
)
goto :EOF
:dof
for /f "Tokens=*" %%a in ('dir %1 /q /a-d^|findstr /L /V /c:"&"^|findstr /I /L /c:"%~nx1"^|shellesc') do (
 set work=%%a
)
call set work=%%work: %~nx1=%%
for /f "Tokens=*" %%a in ('@echo %work:~36%') do (
 set $owner=%%a
)
endlocal&set $owner=%$owner%
goto :EOF
:syntax
set $owner=NONE
@echo Syntax: owner Object