Apply Permissions to Subfolders via PowerShell

I have a variety of "project" folders that each have this format on Windows Server 2012.  I would like a script that will apply specific permissions to the "Admin" folder of all year and job folders for which there are hundreds.  I need to turn off inheritance and then apply the permissions.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy LidbetterCommented:
Hi There,

Here's a great script for updating folder permissions, which also creates the folder if it does not exist. Attached...

I imagine if you could get all the folder names in a get-childitem command eg: For all admin folders.....

$Folders = Get-ChildItem  c:\rootfolder - recurse | where {$_.Attributes -eq 'Directory'} | Where {$ -like "Admin"} | select Fullname

Open in new window

Then call the script against the values like

Foreach ($Folder in $Folders) {c:\scripts\Setfolderpermission.ps1 -path $Folder.Fullname -Access <User\Group> -Permission FullControl

Open in new window

Give it a go....
Guy LidbetterCommented:
Ok.. didn't like the extension so heres the script - save it as setfolderpermission.ps1 to work...

Can't remember who wrote this but its awesome...

param ([string]$Path, [string]$Access, [string]$Permission = ("Modify"), [switch]$help)

function GetHelp() {

$HelpText = @"

NAME: SetFolderPermission.ps1
Sets FolderPermissions for User on a Folder.
Creates folder if not exist.

-Path			Folder to Create or Modify (Required)
-Access			User who should have access (Required)
-Permission		Specify Permission for User, Default set to Modify (Optional)
-help			Prints the HelpFile (Optional)

./SetFolderPermission.ps1 -Path C:\Folder\NewFolder -Access Domain\UserName -Permission FullControl

Creates the folder C:\Folder\NewFolder if it doesn't exist.
Sets Full Control for Domain\UserName

./SetFolderPermission.ps1 -Path C:\Folder\NewFolder -Access Domain\UserName

Creates the folder C:\Folder\NewFolder if it doesn't exist.
Sets Modify (Default Value) for Domain\UserName

./SetFolderPermission.ps1 -help

Displays the help topic for the script

Below Are Available Values for -Permission




function CreateFolder ([string]$Path) {

	# Check if the folder Exists

	if (Test-Path $Path) {
		Write-Host "Folder: $Path" -ForeGroundColor Yellow
	} else {
		Write-Host "Creating $Path" -Foregroundcolor Green
		New-Item -Path $Path -type directory | Out-Null

function SetAcl ([string]$Path, [string]$Access, [string]$Permission) {

	# Get ACL on FOlder

	$GetACL = Get-Acl $Path

	# Set up AccessRule

	$Allinherit = []"ContainerInherit, ObjectInherit"
	$Allpropagation = []"None"
	$AccessRule = New-Object$Access, $Permission, $AllInherit, $Allpropagation, "Allow")

	# Check if Access Already Exists

	if ($GetACL.Access | Where { $_.IdentityReference -eq $Access}) {

		Write-Host "Modifying Permissions For: $Access" -ForeGroundColor Yellow

		$AccessModification = New-Object
		$AccessModification.value__ = 2
		$Modification = $False
		$GetACL.ModifyAccessRule($AccessModification, $AccessRule, [ref]$Modification) | Out-Null
	} else {

		Write-Host "Adding Permission: $Permission For: $Access"


	Set-Acl -aclobject $GetACL -Path $Path

	Write-Host "Permission: $Permission Set For: $Access" -ForeGroundColor Green

if ($help) { GetHelp }

if ($Path -AND $Access -AND $Permission) { 
	CreateFolder $Path 
	SetAcl $Path $Access $Permission

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy LidbetterCommented:
Even easier to find folders...

$Folders = Get-ChildItem  c:\rootfolder -attributes D -recurse -include "Admin" | select Fullname

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.