Search code examples
powershellntfs

Apply folder permissions to folder contents with PowerShell


I'm applying "Read" permissions to a folder for a certain user. I want them to be able to read notepad files inside.

$Acl = Get-Acl "C:\Test"
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule("MyUser","Read","Allow")
$Acl.SetAccessRule($Ar)
Set-Acl "C:\Test" $Acl

My code properly applies the read permissions to the folder (I can manually check the "Security tab to see this), but does not grant access to the files inside. Access is denied when "MyUser" tries to open a notepad file.


Solution

  • You need to use another constructor so you can set InheritanceFlags for containers and leaf objects. Try:

    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule("MyUser","Read","ContainerInherit,ObjectInherit", "None", "Allow")