24

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)?

1

5 Answers 5

26

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
1
  • How can this work with a full directory of files? For example, as a replacement for DIR /Q
    – pbarney
    Jan 11, 2021 at 22:55
4

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.

9
  • 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.
    – codechurn
    Feb 5, 2014 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. Feb 6, 2014 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, 2014 at 21:19
  • Last time I checked, Windows prohibits the transfer of ownership to another account except to the one that performs the transfer. Dec 4, 2014 at 23:55
  • 2
    Microsoft’s security model doesn’t permit to give ownership to someone, only to take it. I am sorry, but this is just nonsense. I am doing exactly this constantly on all sorts of Windows server (back to 2008 R2) and client (back to Windows 7) operating systems. Once you are the owner of a file, you can assign yourself the full range of permissions to do everything with it, including changing the ownership to any other user.
    – Binarus
    Sep 13, 2019 at 8:46
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.

1

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

For example:

DIR *.xlsx /Q
2
  • This is a duplicate of Art's answer.
    – fixer1234
    Apr 28, 2018 at 3:09
  • I'm dealing with some files that are listed as being owned by ... when I run dir /q - even from an elevated cmd window.
    – Dai
    Nov 13, 2023 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .