Link to home
Start Free TrialLog in
Avatar of Rammy Charles
Rammy CharlesFlag for United States of America

asked on

PoweShell Script for Disabling Inheritance on Subfolders

We are looking for a PowerShell script that will simply disable permission inheritance for every subfolder in a parent directory.

Any suggestions would be appreciated.
Avatar of lruiz52
lruiz52
Flag of United States of America image

pipe your get child item into a where item equal a folder then do a foreach  and reset the permission

gci -Path c:\somefolder -recurse | where { $_.PsIsContainer } | % {
	$acl = Get-Item $_ | get-acl
	$acl.SetAccessRuleProtection($true, $true)
	$acl | Set-Acl
}

Open in new window

Avatar of Rammy Charles

ASKER

Thanks just to confirm this will DISABLE inheritance on the child folders?
Yes.

so the script will enumerate all child folders within the parent folder specified.
gci c:\parentfolder - recurse 

Open in new window


It will verify it is only running against folders
| where { $_.PsIsContainer }

Open in new window


Then for each folder it finds no matter how many levels deep it will disable inheritance.
$acl.SetAccessRuleProtection($true, $true)

Open in new window

If we want to limit recursion we can specify how many folders deep we go e.g.:
gci c:\parentfolder\*\*\*

Open in new window

will return two folders deep add an additional \*  for each level deeper you want to recurse.
Thanks. I would only want to disable inheritance on the child folder but not the grandchildren. What would this syntax look like?
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial