{"id":302,"date":"2014-11-23T00:01:00","date_gmt":"2014-11-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/11\/23\/weekend-scripter-manage-ntfs-inheritance-and-use-privileges\/"},"modified":"2019-02-18T10:36:59","modified_gmt":"2019-02-18T17:36:59","slug":"weekend-scripter-manage-ntfs-inheritance-and-use-privileges","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-manage-ntfs-inheritance-and-use-privileges\/","title":{"rendered":"Weekend Scripter: Manage NTFS Inheritance and Use Privileges"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft PFE, Raimund Andree, talks about using Windows PowerShell to disable inheritance on folders.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today Raimund Andree, talks about using Windows PowerShell to disable inheritance on folders. Take it away, Raimund&#8230;<\/p>\n<p>In my previous post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-get-add-and-remove-ntfs-permissions\/\" target=\"_blank\">Use PowerShell to Get, Add, and Remove NTFS Permissions<\/a>, I talked about NTFS inheritance. Inheritance is a fundamental feature of NTFS to keep permissions consistent and easy to manage.<\/p>\n<p>However, there are some scenarios where you want to disable inheritance on folders or find out where it has been disabled. This post explains how this can be achieved by using the NTFSSecurity module.<\/p>\n<h2>Determine inheritance settings<\/h2>\n<p>To determine if a file or folder inherits from its parent, use the <b>Get-NTFSAccessInheritance<\/b> cmdlet (there is also a <b>Get-NTFSAuditInheritance <\/b>cmdlet). There are two ways to specify the file or folder: You can use the <b>Path<\/b> parameter or pipe the file or folder object to <b>Get-NTFSAccessInheritance<\/b>:<\/p>\n<p style=\"margin-left:30px\">Get-NTFSAccessInheritance -Path C:\\Windows<\/p>\n<p style=\"margin-left:30px\">Get-Item C:\\Windows | Get-Inheritance<\/p>\n<p>The output might look like this:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5466.1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5466.1.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>You can easily get the information for a bunch of items by using the built-in <b>Get-ChildItem<\/b> cmdlet, and then pipe all the items to <b>Get-NTFSAccessInheritance<\/b>:<\/p>\n<p style=\"margin-left:30px\">Get-ChildItem c:\\ | Get-NTFSAccessInheritance<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3704.2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3704.2.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Enabling inheritance<\/h2>\n<p>The cmdlet for enabling the inheritance on an object is similar to reading the inheritance information:<\/p>\n<p style=\"margin-left:30px\">Enable-NTFSAccessInheritance -Path .\\Data<\/p>\n<p><span style=\"font-size:12px\">By default, the cmdlet does not return any output unless you use the <\/span><b style=\"font-size:12px\">PassThru<\/b><span style=\"font-size:12px\"> switch.<\/span><\/p>\n<p>The cmdlet provides one additional switch parameter: <b>RemoveExplicitAccessRules<\/b>. When specified, all explicitly assigned access control entries (ACEs) will be removed after enabling the inheritance. This is like setting the folder back to default permissions.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4846.3.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4846.3.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The previous output shows that there were only explicitly assigned ACEs in the Data folder. These were removed after enabling the inheritance and there are only inherited ACEs remaining.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; The <b>RemoveExplicitAccessRules<\/b> switch should be used with care. Using this switch by accident can cause users to not be able to access their resources.<\/p>\n<h3>A common use case for Enable-NTFSAccessInheritance<\/h3>\n<p>If users can alter the ACL of files or folders, they might remove relevant ACEs. The result is that the administrators are no longer able to access the data. Another reason why an administrator cannot access data is that someone has disabled the inheritance and deleted all inherited ACEs.<\/p>\n<p>First you need to use the recursive method provided by <b>Get-ChildItem<\/b> to get all files and folders and filter out the items where inheritance is enabled.The remaining items are piped to <b>Enable-NTFSAccessInheritance<\/b>, which informs about items where inheritance is enabled with the <b>PassThru<\/b> switch.<\/p>\n<p style=\"margin-left:30px\">Get-ChildItem -Recurse | Get-NTFSAccessInheritance | Where-Object { -not $_.InheritanceEnabled } | <br \/>Enable-NTFSAccessInheritance -PassThru<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3603.4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3603.4.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Disabling inheritance<\/h2>\n<p>In NTFSSecurity, you also need to disable inheritance. When enabling inheritance you have to determine what to do with explicit ACES (usually you keep them). When disabling inheritance, the inherited ACEs can be converted into explicit ones or they can be removed. This is controlled by the <b>RemoveInheritedAccessRules<\/b> switch.<\/p>\n<p>In the following example, the inheritance for the folder GPO is enabled. The folder is inheriting four ACEs from the parent (drive D). After disabling inheritance, the ACEs still exist as explicit ACEs.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5314.5.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5314.5.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; When using the <b>RemoveInheritedAccessRules<\/b> switch, you need to make sure that the access is guaranteed after disabling the inheritance. If an item does not have explicit ACEs assigned and you remove all inherited ACEs, nobody will be able to access the file. However, an administrator can always use the back-up privilege or take ownership to get access again.<\/p>\n<h2>Using privileges<\/h2>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> There was a big change in Windows 8 and Windows Server&nbsp;2012 regarding how privileges can be used. The features described in this section work only on Windows 8.1, Windows&nbsp;8, Windows Server&nbsp;2012&nbsp;R2, and Windows Server&nbsp;2012.<\/p>\n<p>Sometimes permissions are hosed and access is denied, even if you are the mighty administrator. In this case, you cannot even read the existing ACL. Of course, the administrator can always take the ownership of an object, but this erases the existing ACL of the object. This is bad because you cannot determine by the ACL the people who need to have access.<\/p>\n<p>Windows has a very handy concept of privileges. A privilege represents the right of an account, such as a user or group account, to perform various system-related operations on the local computer, such as shutting down the system, loading device drivers, or changing the system time. Privileges differ from access rights in two ways:<\/p>\n<ul>\n<li>Privileges control access to system resources and system-related tasks, whereas access rights control access to securable objects.<\/li>\n<li>A system administrator assigns privileges to user and group accounts, whereas the system grants or denies access to a securable object based on the access rights granted in the ACEs in the object&#039;s DACL.<\/li>\n<\/ul>\n<p>When dealing with files and folders, three privileges are worth looking at:<\/p>\n<ul>\n<li>Back up files and directories: <br \/> This user right determines which users can bypass file and directory, registry, and other persistent object permissions for the purposes of backing up the system. By default, Administrators and Backup Operators can make use of this privilege.<\/li>\n<li>Restore files and directories: <br \/> This security setting determines which users can bypass file, directory, registry, and other persistent objects permissions when restoring backed up files and directories, and it determines which users can set valid security principals as the owner of an object.\n<p><span style=\"font-size:12px\">Specifically, this user right is similar to granting the following permissions to the user or group in question on all files and folders on the system:<\/span><\/p>\n<ul>\n<li>Traverse Folder\/Execute File<\/li>\n<li>Write<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"margin-left:30px\"><span style=\"font-size:12px\">&nbsp; &nbsp; By default Administrators and Backup Operators have this privilege assigned.<\/span><\/p>\n<ul>\n<li>Take ownership of files or other objects: <br \/> This security setting determines which users can take ownership of any securable object in the system, including Active Directory objects, files and folders, printers, registry keys, processes, and threads. By default, this privilege is assigned to Administrators.<\/li>\n<\/ul>\n<p>For a list of privileges, refer to <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc740217(v=WS.10).aspx\" target=\"_blank\">Privileges<\/a> in the TechNet Library.<\/p>\n<p>To create the situation described previously, you can use the following command sequence:<\/p>\n<p style=\"margin-left:30px\">Set-NTFSOwner .\\Data -Account &#039;NT AUTHORITY\\SYSTEM&#039;<\/p>\n<p style=\"margin-left:30px\">Disable-NTFSAccessInheritance .\\Data -RemoveInheritedAccessRules<\/p>\n<p>The first command assigns the ownership of the Data folder to the SYSTEM account, and the second command disables the inheritance. Now only the SYSTEM account can access the item.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8030.6.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8030.6.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Windows Explorer is not helpful either:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5857.7.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5857.7.PNG\" alt=\"Image of message\" title=\"Image of message\" \/><\/a><\/p>\n<h2>Enabling privileges<\/h2>\n<p>The NTFSSecurity module provides the <b>Enable-Privileges<\/b> cmdlet. This cmdlet enables the privileges Backup, Restore, and Security (if you have them). You can get a list of available privileges by using <b>Get-Privilege<\/b>.<\/p>\n<p>Running <b>Get-Privilege<\/b> in a non-elevated Windows PowerShell console should return something like this:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1346.8.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1346.8.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The privileges Backup, Restore, and Security are missing. If you run the same command in an elevated Windows PowerShell console, the list gets much longer:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0474.9.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0474.9.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>All these privileges are disabled by default. Having them enabled all the time would be quite dangerous and would increase ones scope of action too much. However in some scenarios, it is required to make use of privileges, for example, when doing backup jobs, data migrations, or permission cleanups.<\/p>\n<p>To enable the privileges, call <b>Enable-Privileges<\/b>. You will get a warning message that informs you about the enabled privileges.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; If you call the cmdlet in a non-elevated Windows PowerShell console, and hence, you do not hold the proper privileges, the following error message returns: <br \/> Enable-Privileges: Could not enable requested privileges. Cmdlets of NTFSSecurity will only work on resources you have access to.<\/p>\n<p>After calling <b>Enable-Privileges<\/b>, you can check the state of the privileges by using <b>Get-Privileges<\/b>.<\/p>\n<h3>Using enabled privileges<\/h3>\n<p>Enabling the privileges is pretty much all you need to do. The process that has enabled them uses them automatically.<\/p>\n<p>After removing the permissions from the Data folder, there is no way to access it or display the owner or ACL. However, after enabling the privileges, Windows bypasses the ACL and grants you full access (thanks to the Backup and Restore privilege).<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/10.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/10.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><b>Get-NTFSAccess<\/b> does not return any data because there is no ACE in the ACL. Remember that all ACEs have been removed by disabling the inheritance.<\/p>\n<p>Now you can take the ownership and enable inheritance again:<\/p>\n<p style=\"margin-left:30px\">Set-NTFSOwner -Path .\\Data -Account BUILTIN\\Administrators<\/p>\n<p style=\"margin-left:30px\">Enable-NTFSInheritance -Path .\\Data<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/11.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/11.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now the access is how it should be again.<\/p>\n<h2>Disabling privileges<\/h2>\n<p>If you no longer need the privileges, it is strongly recommended to disable them. Use the command <b>Disable-Privileges<\/b> for this.<\/p>\n<p>After calling <b>Disable-Privileges<\/b>, you can check the state of the privileges by using <b>Get-Privileges<\/b>.<\/p>\n<p>~Raimund<\/p>\n<p>Thank you again, Raimund, for an informative blog post.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PFE, Raimund Andree, talks about using Windows PowerShell to disable inheritance on folders. Microsoft Scripting Guy, Ed Wilson, is here. Today Raimund Andree, talks about using Windows PowerShell to disable inheritance on folders. Take it away, Raimund&#8230; In my previous post, Use PowerShell to Get, Add, and Remove NTFS Permissions, I talked about [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,544,3,63,61,45],"class_list":["post-302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-raimund-andree","tag-scripting-guy","tag-security","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PFE, Raimund Andree, talks about using Windows PowerShell to disable inheritance on folders. Microsoft Scripting Guy, Ed Wilson, is here. Today Raimund Andree, talks about using Windows PowerShell to disable inheritance on folders. Take it away, Raimund&#8230; In my previous post, Use PowerShell to Get, Add, and Remove NTFS Permissions, I talked about [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/302","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/302\/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=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}