How Can I Determine the Owner of a File?

How Can I Determine the Owner of a File?

  • Comments 3
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there a way to determine the owner of a file by using a script?

-- BD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BD. As a matter of fact there is a way to use a script to determine the owner of the file, though it’s understandable why you might not be able to find that information on your own. After all, there are two main scripting interfaces used for managing files: the Script Runtime’s FileSystemObject and the WMI class CIM_DataFile. Seeing as how neither of these interfaces includes a property or method for determining file ownership, the logical conclusion would be, “Oh, I guess you can’t do this after all.”

But you know how it is with scripting: things can often be done, though not the way you might logically expect to do them. In this case, you need to use WMI’s Win32_LogicalFileSecuritySetting class in conjunction with the Win32_LogicalFileOwner association class to determine file ownership. As you might expect, the LogicalFileSecuritySetting class grabs security information from a file. What it can’t do, however, is tell you the name of the file owner. That’s where the Win32_LogicalFileOwner class comes into play: it takes the owner’s SID (security identifier) and relays it to the Win32_SID class. The Win32_SID class can then lookup and report the owner name and domain.

Confused? We don’t blame you; association classes are not the most intuitive thing ever created. Fortunately, however, you don’t need to understand how association classes work; just rest assured that they do work. For example, here’s a script that reports the owner of the file C:\Scripts\My_script.vbs:

On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

strFile = "C:\Scripts\My_script.vbs"

Set colItems = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _ 
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")

For Each objItem in colItems
    Wscript.Echo objItem.ReferencedDomainName
    Wscript.Echo objItem.AccountName
Next

Looks crazy, but it will do the job. And what if you need to get the owner of a different file? No problem: just set the value of the variable strFile to the complete path of that file.

And don’t let the names mislead you: these two classes can also be used to determine the owner of a folder. For example, this script reports back the owner of the folder C:\Scripts:

On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

strFile = "C:\Scripts"

Set colItems = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _ 
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")

For Each objItem in colItems
    Wscript.Echo objItem.ReferencedDomainName
    Wscript.Echo objItem.AccountName
Next
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Why not just use one CMD line?

    > dir /q

    DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]

     [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

     /Q          Display the owner of the file.

  • So here's my situation: Migration scenario using Quest Migration Manager for Active Directory. A file server where Users' My Docs are being redirected to. Per the GUI (Windows Explorer), when viewing the Ownership on a folder, shows SOURCE\Username BUT both the script and the DIR /Q command show TARGET\Username .. Why does the GUI report something different?

    Interesting...

    Is Owner a single value attribute or a multivalue attribute?

    What would account for this?

    Thanks in advance if anybody can provide some insight!

    Kind Regards,

     - Rick Gregson

       rickgregson864@convergenttechonline.com

  • Of course this does not work on a Novell network, where the User is always reported to be "Everyone".