Search code examples
windowspowershellaclwindows-server-2008-r2ntfs

Removing all ACL on folder with powershell


I'm pretty new to powershell scripting (nearly 1 month since I started learning powershell.)

I'm currently working on a script with powershell 2.0 to clean folder NTFS ACL. I want to delete every acl except the administrator one.

My problem is that I can't find a way to delete every acl that are not administrator, without knowing them.

So I came here to sought for powershell pro.


Solution

  • This code remove acl :

    $acl = Get-Acl \\remote_server\share_folder\HAL.9000
    $acl.Access | %{$acl.RemoveAccessRule($_)}
    

    This code add administrator acl :

    #BUILTIN administrator
    
    $acl = Get-Acl \\remote_server\share_folder\HAL.9000
    $permission  = "BUILTIN\Administrators","FullControl", "ContainerInherit,ObjectInherit","None","Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    Set-Acl \\remote_server\share_folder\HAL.9000 $acl
    
    #Domain controller administrator
    
    $acl = Get-Acl \\remote_server\share_folder\HAL.9000
    $permission  = "DOMAINCONTROLLER\Administrators","FullControl", "ContainerInherit,ObjectInherit","None","Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    Set-Acl \\remote_server\share_folder\HAL.9000 $acl
    

    Hope this will help someone :)