{"id":68203,"date":"2006-01-11T15:12:00","date_gmt":"2006-01-11T15:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/11\/how-can-i-take-ownership-of-a-file-or-folder-by-using-a-script\/"},"modified":"2006-01-11T15:12:00","modified_gmt":"2006-01-11T15:12:00","slug":"how-can-i-take-ownership-of-a-file-or-folder-by-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-take-ownership-of-a-file-or-folder-by-using-a-script\/","title":{"rendered":"How Can I Take Ownership of a File or Folder By Using a Script?"},"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! How can I take ownership of a file or folder using a script?<BR><BR>&#8212; RV<\/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, RV. Interesting question. In the past, the Scripting Guys have tried to take ownership of a Porsche 911, beachfront property in Hawaii, and the New York Yankees; all of those efforts failed. Considering our track record, we weren\u2019t sure what would happen if we tried to take ownership of a file or folder. (Although as long as we weren\u2019t issued yet <I>another<\/I> restraining order we figured it couldn\u2019t be too bad.)<\/P>\n<P>As it turns out, taking ownership of a file or folder is child\u2019s play. (Assuming, of course, that the child in question has been granted the NTFS permission enabling him or her to take ownership of the file or folder.) The following script might not be as good as a beachfront property in Hawaii, but it\u2019s awfully close:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Scripts'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    objFolder.TakeOwnershipEx\nNext\n<\/PRE>\n<P>If you\u2019re thinking, \u201cMan, this looks like a run-of-the-mill WMI script,\u201d well, you\u2019re right: it <I>is<\/I> a run-of-the-mill WMI script. We start off by connecting to the WMI service on the local computer (although we could just as easily run this script against a remote computer). We then use this line to retrieve a collection of folders; in this case, all the folders that have the name C:\\Scripts:<\/P><PRE class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Scripts'&#8221;)\n<\/PRE>\n<P>Two things to note here. First, because we want to take ownership of a folder we use the <B>Win32_Directory<\/B> class. What if we wanted to take ownership of a <I>file<\/I>? Well, then we\u2019d need to use the CIM_DataFile class. Here\u2019s an example of a query that would bind us to the file C:\\Scripts\\Test.vbs:<\/P><PRE class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where Name = &#8216;C:\\\\Scripts\\\\Test.vbs'&#8221;)\n<\/PRE>\n<P>Second, note that we have to double-up any \\ characters in our path; thus <B>C:\\Scripts<\/B> becomes <B>C:\\\\Scripts<\/B>. That\u2019s because the \\ is a reserved character in WMI; because of that, any time we need to use a \\ in a WQL query we have to \u201cescape\u201d that character. In this case, \u201cescape\u201d is just a fancy way of saying use two \\\u2019s instead of one. (Or, in the case of a UNC path, four \\\u2019s instead of two; thus \\\\atl-fs-01\\public becomes \\\\\\\\atl-fs-01\\\\public.)<\/P>\n<P>As you know, calling the <B>ExecQuery<\/B> method gives us a collection of folders (although this particular collection contains only a single folder, C:\\Scripts). To take ownership of each item in the collection we set up a For Each loop and then, for each of those items, call the <B>TakeOwnershipEx<\/B> method:<\/P><PRE class=\"codeSample\">For Each objFolder in colFolders\n    objFolder.TakeOwnershipEx\nNext\n<\/PRE>\n<P>The TakeOwnershipEx method enables us to take ownership not only of the folder C:\\Scripts but also any subfolders found in C:\\Scripts. If you don\u2019t want to take ownership of the subfolders then use the <B>TakeOwnership<\/B> method instead. In that case your For Each loop would look like this:<\/P><PRE class=\"codeSample\">For Each objFolder in colFolders\n    objFolder.TakeOwnership\nNext\n<\/PRE>\n<P>So there you go, RV. The next time some guy whizzes past you in a Porsche remember this: he might have the fancy sports car, but you have a script that enables you to take ownership of a file or folder. If that doesn\u2019t wipe the smile off the face of Mr. Porsche Driver we don\u2019t know what will.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I take ownership of a file or folder using a script?&#8212; RV Hey, RV. Interesting question. In the past, the Scripting Guys have tried to take ownership of a Porsche 911, beachfront property in Hawaii, and the New York Yankees; all of those efforts failed. Considering our track record, we [&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":[38,11,89,3,63,12,5],"class_list":["post-68203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-resource-ownership","tag-scripting-guy","tag-security","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I take ownership of a file or folder using a script?&#8212; RV Hey, RV. Interesting question. In the past, the Scripting Guys have tried to take ownership of a Porsche 911, beachfront property in Hawaii, and the New York Yankees; all of those efforts failed. Considering our track record, we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68203","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=68203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68203\/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=68203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}