14

Is there a way to display the owner of folders and files from the command line in Windoews 7 command prompt?

Can you change the owner of a folder or file to some "arbitrary" user which is not your own username?

I have some folders (and files) that are probably left over from an app that I have removed. If I try to view the contents of the folders I am that told I don't have permission to do so, even if I am running as an "Administrator".

I can "Take ownership" (I assume this will be successful but haven't tried it yet) of the folders (files), but if I need to revert the ownership to the previous owner, I need to know the username of the original owner, and I need to be able to "give ownership" to that user.

Is it possible to do this from the Windows command prompt (or if not, from a GUI-Tool)?

17

You can take ownership from the command line via the takeown command and via the Windows GUI.

You can view the owner of a file/folder by using the DIR with a /q parameter

You can view (and take) ownership via the Windows GUI by right clicking the object in Windows Explorer (file or folder), selecting Properties and then navigating to the Security tab. On the Security tab, click the Advanced button and on the subsequently displayed Advanced Security Settings dialog, navigate to the Owner tab.

Once you have taken ownership of a file/folder, Windows does not track the previous owner, so there is no way to revert back to the previous owner. Also, there is no concept of ownership or file permissions if you are working with a file system type that does not support these extended attributes such as FAT16, FAT32, exFAT, etc.

5

You can use wmic to query the ownership information like this:

wmic path Win32_LogicalFileSecuritySetting where Path="C:\\windows\\winsxs" ASSOC /RESULTROLE:Owner /ASSOCCLASS:Win32_LogicalFileOwner /RESULTCLASS:Win32_SID

Don't use dir since the ownership info may be clipped, as with this example directory.

To get an output formatted with DOMAIN\USER you can use the following batch script:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
REM Escpe the backslash with \\
SET ESCAPED=%~f1
SET ESCAPED=!ESCAPED:\=\\!

wmic path Win32_LogicalFileSecuritySetting where Path="!ESCAPED!" ASSOC /RESULTROLE:Owner /ASSOCCLASS:Win32_LogicalFileOwner /RESULTCLASS:Win32_SID > "%temp%\wmi.tmp"

for /F "skip=2 delims=€" %%G in ('type %temp%\wmi.tmp') do (call     :process_wmioutput "%%G")
goto :continue
:process_wmioutput
SET UNDELIMITED=%1
SET DELIMITED=!UNDELIMITED:  =€!
FOR /F "delims=€ tokens=10,12" %%G in ("!DELIMITED!") DO (ECHO %%H\%%G)
exit /B

:continue
3

SubInACL.exe allows you to set the owner. The syntax looks something like:

SubInACL /file filename /setowner=NewOwner

If you don't have it, you can download it from Microsoft.

Of course you have to have the rights to be able to do this.

2

cacls and icalcs can edit permissions and takeown allows to take ownership. AFAIK they exist in Windows 7 as well. Typically, once you do a takeown, you follow it up with cacls or icalcs to grant yourself permissions to the object.

Microsoft’s security model doesn’t permit to give ownership to someone, only to take it. That way an admin (or otherwise privileged user) cannot take ownership of a file inaccessible to her directly, access or modify it, and give it back to the original owner without notice to the original owner.

Edit: Credit goes to Art for the description of the use of takeown.

  • 1
    cacls does not allow one to change or view the owner of a file. You would use takeown to do that. Typically, once you do a takeown, you follow it up with calcs or icacls to grant yourself permissions to the object. – Art Feb 5 '14 at 19:28
  • Thanks, you're entirely right. I guess it shows that I've been out of the Microsoft world for too long to remember those things. I'll edit my answer accordingly. – David Foerster Feb 6 '14 at 20:30
  • 2
    I know this is old, but I thought that I would add that you can set the owner to someone else using icacls. icacls <path> /SETOWNER <name> works. Of course you need to be the owner or have appropriate permissions before you can do that so you could enact the scenario of taking ownership and giving it back provided you have the ability to take ownership in the first place. – palehorse Dec 3 '14 at 21:19
  • Last time I checked, Windows prohibits the transfer of ownership to another account except to the one that performs the transfer. – David Foerster Dec 4 '14 at 23:55
  • @DavidFoerster - According to this: Restoring 'TrustedInstaller' as owner for executable in Windows folder, it appears that it will work. I haven't tried it myself yet. – Kevin Fegan Nov 17 '15 at 11:11
-1

If using just DIR in the Microsoft world, try the /Q option.

For example:

DIR *.xlsx /Q
  • This is a duplicate of Art's answer. – fixer1234 Apr 28 '18 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.