{"id":71273,"date":"2004-10-07T11:15:00","date_gmt":"2004-10-07T11:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/07\/how-can-i-determine-the-owner-of-a-file\/"},"modified":"2004-10-07T11:15:00","modified_gmt":"2004-10-07T11:15:00","slug":"how-can-i-determine-the-owner-of-a-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-owner-of-a-file\/","title":{"rendered":"How Can I Determine the Owner of a File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Is there a way to determine the owner of a file by using a script?<BR><BR>&#8212; BD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, BD. As a matter of fact there <I>is<\/I> a way to use a script to determine the owner of the file, though it\u2019s 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\u2019s 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, \u201cOh, I guess you can\u2019t do this after all.\u201d<\/P>\n<P>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\u2019s 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\u2019t do, however, is tell you the name of the file owner. That\u2019s where the Win32_LogicalFileOwner class comes into play: it takes the owner\u2019s 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.<\/P>\n<P>Confused? We don\u2019t blame you; association classes are not the most intuitive thing ever created. Fortunately, however, you don\u2019t need to understand how association classes work; just rest assured that they <I>do<\/I> work. For example, here\u2019s a script that reports the owner of the file C:\\Scripts\\My_script.vbs:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n      &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>strFile = &#8220;C:\\Scripts\\My_script.vbs&#8221;<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='&#8221; &amp; strFile &amp; &#8220;&#8216;}&#8221; _ \n        &amp; &#8221; WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.ReferencedDomainName\n    Wscript.Echo objItem.AccountName\nNext\n<\/PRE>\n<P>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.<\/P>\n<P>And don\u2019t let the names mislead you: these two classes can also be used to determine the owner of a <I>folder<\/I>. For example, this script reports back the owner of the folder C:\\Scripts:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n      &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>strFile = &#8220;C:\\Scripts&#8221;<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='&#8221; &amp; strFile &amp; &#8220;&#8216;}&#8221; _ \n        &amp; &#8221; WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.ReferencedDomainName\n    Wscript.Echo objItem.AccountName\nNext\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there a way to determine the owner of a file by using a script?&#8212; BD Hey, BD. As a matter of fact there is a way to use a script to determine the owner of the file, though it\u2019s understandable why you might not be able to find that information on [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[89,3,63,5],"class_list":["post-71273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-resource-ownership","tag-scripting-guy","tag-security","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there a way to determine the owner of a file by using a script?&#8212; BD Hey, BD. As a matter of fact there is a way to use a script to determine the owner of the file, though it\u2019s understandable why you might not be able to find that information on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71273\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}