Solved

PoweShell Script for Disabling Inheritance on Subfolders

Posted on 2014-11-04
6
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

696 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