{"id":55773,"date":"2008-04-16T01:08:00","date_gmt":"2008-04-16T01:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/16\/hey-scripting-guy-how-can-i-use-windows-powershell-to-determine-the-owner-of-a-file\/"},"modified":"2008-04-16T01:08:00","modified_gmt":"2008-04-16T01:08:00","slug":"hey-scripting-guy-how-can-i-use-windows-powershell-to-determine-the-owner-of-a-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-windows-powershell-to-determine-the-owner-of-a-file\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use Windows PowerShell to Determine the Owner of a File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! Is it possible to determine the owner of a file using Windows PowerShell?<BR>&#8212; GF<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GF. Well, today is April 15<SUP>th<\/SUP>, which, in the US, can mean only one thing: it\u2019s time to celebrate the birthday of Italian mathematician <A href=\"http:\/\/en.wikipedia.org\/wiki\/Pietro_Cataldi\" target=\"_blank\"><B>Pietro Antonio Cataldi<\/B><\/A>, best known for discovering the sixth and seventh Mersenne primes. Pietro, who developed the first notation for continued fractions, was born on this day in 1552. Happy birthday, Pietro!<\/P>\n<P>Coincidentally, April 15<SUP>th<\/SUP> is also Tax Day in the US, the last day on which Americans can submit their income tax returns for the previous year. Needless to say, for many Americans April 15<SUP>th<\/SUP> is a very stressful day. For other Americans, however, April 15<SUP>th<\/SUP> isn\u2019t the least bit stressful; that\u2019s because the US has a long history of people who believe that the government has no right to collect income taxes and therefore decide not to pay their taxes. For example, in 1997 actor Wesley Snipes (recently convicted on three counts of failure to pay income tax) reported an income of $19,238,192. Not only did Wesley decline to pay any taxes on that income, he actually demanded a refund of $7,360,755. Interestingly enough, <A href=\"http:\/\/www.nytimes.com\/2008\/02\/01\/business\/01cnd-tax.html?_r=1&amp;hp&amp;oref=slogin\" target=\"_blank\"><B>his own lawyers<\/B><\/A> termed his positions on income tax \u201ckooky,\u201d \u201ccrazy\u201d and \u201cdead wrong.\u201d<\/P>\n<P>Which, coincidentally enough, are the exact same phrases that were sprinkled throughout the mid-year performance review of the Scripting Guy who writes this column.<\/P>\n<P>Oh, and did we mention that Wesley Snipes was recently convicted on three counts of failure to pay income tax? That\u2019s usually what happens to people who decline to pay their taxes or file a tax return.<\/P>\n<P>As it turns out, the Scripting Guy who writes this column isn\u2019t stressing out today, either; that\u2019s because he submitted his tax return well in advance of today\u2019s deadline. (On Sunday, April 13<SUP>th<\/SUP>, to be exact.) Admittedly, that might sound like he was cutting it a little close. He wasn\u2019t concerned, however, because he knew he could complete his tax return in less than hour; needless to say, it doesn\u2019t take him anywhere near as long to count his money as it takes Wesley Snipes to count his.<\/P>\n<P>And no, that\u2019s not because the Scripting Guy who writes this column is a really fast counter.<\/P>\n<P>Best of all, getting his taxes done early turned out to have multiple benefits for the Scripting Guy who writes this column. For one thing, submitting his tax return helped him avoid going to prison for income tax evasion; that\u2019s usually a plus. For another, filing early also gave him time to figure out how to determine the owner of a file (or folder) using Windows PowerShell. <\/P>\n<P>Although, in all honesty, he didn\u2019t need all that much time to do that, either:<\/P><PRE class=\"codeSample\">Get-Acl C:\\Scripts\\Test.txt\n<\/PRE>\n<P>Believe it or not, that\u2019s the entire script; all we have to do to determine the owner of a file is call the <B>Get-Acl<\/B> cmdlet, passing Get-Acl the path to the file in question. In turn, Get-Acl will report back information similar to this:<\/P><PRE class=\"codeSample\">Directory: Microsoft.PowerShell.Core\\FileSystem::C:\\Scripts<\/p>\n<p>Path                          Owner                         Access\n&#8212;-                          &#8212;&#8211;                         &#8212;&#8212;\nTest.txt                      FABRIKAM\\kenmyer              BUILTIN\\Administrators Allow  FullCo&#8230;\n<\/PRE>\n<P>Not bad, huh? If all you care about is the name of the owner then pipe the results to the <B>Select-Object<\/B> cmdlet, like so:<\/P><PRE class=\"codeSample\">Get-Acl C:\\Scripts\\Test.txt | Select-Object Owner\n<\/PRE>\n<P>That will give you information similar to this:<\/P><PRE class=\"codeSample\">Owner\n&#8212;&#8211;\nFABRIKAM\\kenmyer\n<\/PRE>\n<P>Or, if you\u2019d like to see the complete security descriptor, pipe the output to the <B>Format-List<\/B> cmdlet:<\/P><PRE class=\"codeSample\">Get-Acl C:\\Scripts\\Test.txt | Format-List\n<\/PRE><PRE class=\"codeSample\">Path   : Microsoft.PowerShell.Core\\FileSystem::C:\\Scripts\\Test.txt\nOwner  : FABRIKAM\\kenmyer\nGroup  : FABRIKAM\\Domain Users\nAccess : BUILTIN\\Administrators Allow  FullControl\n         NT AUTHORITY\\SYSTEM Allow  FullControl\n         FABRIKAM\\kenmyer Allow  FullControl\n         BUILTIN\\Users Allow  ReadAndExecute, Synchronize\nAudit  :\nSddl   : O:S-1-5-21-1454471165-1004336348-1606980848-8183G:DUD:AI(A;ID;FA;;;BA)(A;ID;FA;;;SY)\n         (A;ID;FA;;;S-1-5-21-1454471165-1004336348-1606980848-8183)(A;ID;0x1200a9;;;BU)\n<\/PRE>\n<P>If we had to guess, we\u2019d guess that Wesley Snipes didn\u2019t <I>mean<\/I> to ignore the April 15<SUP>th<\/SUP> deadline for filing his tax return. He probably just got caught up in the fun and excitement of working with file ownership scripts, and forgot all about paying his taxes.<\/P>\n<P>For example, suppose Wesley wanted to get a list of owners for all the files in the folder C:\\Scripts. That\u2019s no problem; after all, the Get-Acl cmdlet <I>does<\/I> accept wildcard characters:<\/P><PRE class=\"codeSample\">Get-Acl C:\\Scripts\\*.*\n<\/PRE><PRE class=\"codeSample\">    Directory: Microsoft.PowerShell.Core\\FileSystem::C:\\Scripts<\/p>\n<p>Path                          Owner                         Access\n&#8212;-                          &#8212;&#8211;                         &#8212;&#8212;\nExample.txt                   FABRIKAM\\kenmyer              BUILTIN\\Administrators Allow  FullCo&#8230;\nTest.txt                      FABRIKAM\\pilarackerman        BUILTIN\\Administrators Allow  FullCo&#8230;\nTrial.txt                     FABRIKAM\\kenmyer              BUILTIN\\Administrators Allow  FullCo&#8230;\n<\/PRE>\n<P>Pretty cool, huh? Of course, while Get-Acl <I>does<\/I> accept wildcard characters, what it <I>doesn\u2019t<\/I> accept is some sort of <B>\u2013recurse<\/B> parameter that would enable you to retrieve the owners of all the files located in any subfolders of C:\\Scripts. But that\u2019s OK, too; after all, the <B>Get-ChildItem<\/B> cmdlet <I>does<\/I> accept the \u2013recurse parameter. That means we can retrieve the file owners for all the files in C:\\Scripts and its subfolders by using this command:<\/P><PRE class=\"codeSample\">Get-ChildItem C:\\Scripts -recurse | ForEach-Object {Get-Acl $_.FullName}\n<\/PRE>\n<P>There\u2019s nothing particularly complicated about that command, either: we simply use Get-ChildItem and the \u2013recurse parameter to retrieve the collection of files found in C:\\Scripts and its subfolders, then pipe that collection to the <B>ForEach-Object<\/B> cmdlet. In turn, we ask ForEach-Object to run the Get-Acl cmdlet against each and every file in that collection, using the value of the <B>FullName<\/B> property as Get-Acl\u2019s file path parameter.<\/P>\n<P>Is that going to work? Hey, come on: have you ever known the Scripting Guys to do something that <I>didn\u2019t<\/I> work?<\/P>\n<P>Well, OK. But the command we showed you <I>will<\/I> work. Promise.<\/P>\n<P>Who would have guessed that file ownership scripting could be so much fun, eh? In fact, like Wesley Snipes, we\u2019re having such a good time today we thought we\u2019d try one more script. It <I>is<\/I> pretty cool that you can determine the owner of a file by running a simple little Windows PowerShell script. But you know what would be <I>really<\/I> cool? It would be really cool if you could take ownership of a file by running a simple little Windows PowerShell script. You know, maybe a script like this one:<\/P><PRE class=\"codeSample\">$objUser = New-Object System.Security.Principal.NTAccount(&#8220;fabrikam&#8221;, &#8220;kenmyer&#8221;)\n$objFile = Get-Acl C:\\Scripts\\Test.txt\n$objFile.SetOwner($objUser)\nSet-Acl -aclobject $objFile -path C:\\Scripts\\Test.txt\n<\/PRE>\n<P>Much like the Scripting Guy who writes this column\u2019s income for the year 2007, there\u2019s really not much to this script. In line 1 we use the <B>New-Object<\/B> cmdlet to create an instance of the <B>System.Security.Principal.NTAccount<\/B> class, a .NET Framework class used to represent a user account. When creating an instance of this class we need to pass two parameters: the name of our domain (fabrikam) and the name of our user account (kenmyer).<\/P>\n<TABLE class=\"dataTable\" id=\"EZG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. That\u2019s a good question, and as far as we know the answer is this: assuming you want to stay out of jail then, yes, you <I>do<\/I> have to pay income tax in the US. As for your other question, the answer is no: although you can take ownership of a file using Windows PowerShell, we don\u2019t believe that you can give ownership of a file to someone else. To transfer ownership to another user you\u2019ll need to use the Windows Resource Kit utility <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=E8BA3E56-D8FE-4A91-93CF-ED6985E3927B&amp;displaylang=en\" target=\"_blank\"><B>Subinacl.exe<\/B><\/A>.<\/P>\n<P>As far as we know, anyway.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we create an instance of the NTAccount class we use the Get-Acl cmdlet to retrieve the security descriptor from the file C:\\Scripts\\Test.txt; that\u2019s what we do here:<\/P><PRE class=\"codeSample\">$objFile = Get-Acl C:\\Scripts\\Test.txt\n<\/PRE>\n<P>Once we have the security descriptor we can use the <B>SetOwner<\/B> method to assign ourselves ownership of the file:<\/P><PRE class=\"codeSample\">$objFile.SetOwner($objUser)\n<\/PRE>\n<P>Well, sort of. What the SetOwner method does is assign ownership to the virtual copy of the security descriptor that we retrieved using Get-Acl. To take ownership of the actual file itself we need to use the following <B>Set-Acl<\/B> command:<\/P><PRE class=\"codeSample\">Set-Acl -aclobject $objFile -path C:\\Scripts\\Test.txt\n<\/PRE>\n<P><I>That<\/I> should give you ownership of the file.<\/P>\n<P>Give these scripts a try, GF; with any luck they should help you with your management of files and file owners. As for the Scripting Guy who writes this column, he\u2019s going to take the rest of the day off; after all, it <I>is<\/I> Pietro Antonio Cataldi\u2019s birthday, you know. <\/P>\n<TABLE class=\"dataTable\" id=\"EGAAC\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Why isn\u2019t Pietro\u2019s birthday a national holiday in the US? Beats us; after all, the man <I>did<\/I> hold the record for the largest known prime number for 184 years, until Leonhard Euler came along in 1772 and discovered that 2<SUP>31<\/SUP> &#8211; 1 was the eighth Mersenne prime. The largest known Mersenne prime number as of this writing is 2<SUP>32,582,657<\/SUP>\u22121, which \u2013 by yet another amazing coincidence \u2013 is also the amount of money the Scripting Guy who writes this column requested as an income tax refund for the year 2007. We\u2019ll let you know how that goes.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to determine the owner of a file using Windows PowerShell?&#8212; GF Hey, GF. Well, today is April 15th, which, in the US, can mean only one thing: it\u2019s time to celebrate the birthday of Italian mathematician Pietro Antonio Cataldi, best known for discovering the sixth and seventh Mersenne primes. [&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,45],"class_list":["post-55773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-resource-ownership","tag-scripting-guy","tag-security","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is it possible to determine the owner of a file using Windows PowerShell?&#8212; GF Hey, GF. Well, today is April 15th, which, in the US, can mean only one thing: it\u2019s time to celebrate the birthday of Italian mathematician Pietro Antonio Cataldi, best known for discovering the sixth and seventh Mersenne primes. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55773","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=55773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55773\/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=55773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}