Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PoweShell Script for Disabling Inheritance on Subfolders

Posted on 2014-11-04
6
Medium Priority
?
1,031 Views
Last Modified: 2014-11-25
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.
0
Comment
Question by:GR JN
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:lruiz52
ID: 40422767
0
 
LVL 29

Expert Comment

by:becraig
ID: 40422914
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

0
 

Author Comment

by:GR JN
ID: 40424171
Thanks just to confirm this will DISABLE inheritance on the child folders?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:becraig
ID: 40424566
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.
0
 

Author Comment

by:GR JN
ID: 40424621
Thanks. I would only want to disable inheritance on the child folder but not the grandchildren. What would this syntax look like?
0
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40424645
gci -Path c:\somefolder\* | ? { $_.PsIsContainer } | % {
	$acl = Get-Item $_ | get-acl
	$acl.SetAccessRuleProtection($true, $true)
	$acl | Set-Acl
}
                         

Open in new window

0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Loops Section Overview

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question