Created
June 13, 2018 00:19
-
-
Save brettmillerb/46512520250d3689dd3e60c426e6c9de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-CustomACL { | |
<# | |
.SYNOPSIS | |
Sets a Custom ACL on a provided folder | |
.DESCRIPTION | |
Sets ACl permissions on a provided folder recursively | |
.PARAMETER User | |
The user to add to the ACL | |
.PARAMETER Rights | |
FilesystemRights to grant to the user account. You can use Get-Help Set-CustomACL to see valid values | |
.PARAMETER ACLType | |
The ACL Type to provide to the user - Valid values are: | |
Allow, Deny | |
.PARAMETER StartingDir | |
Directory on which to set the ACL permissions | |
.EXAMPLE | |
Set-CustomACL -User "domain\Brettm" -Rights FullControl -ACLType Allow -StartingDir C:\Temp | |
.EXAMPLE | |
Set-CustomACL -User "domain\Brettm" -Rights ReadAndExecute, ReadAttributes, CreateDirectories, CreateFiles -ACLType Allow -StartingDir C:\Temp | |
.NOTES | |
General notes | |
#> | |
[CmdletBinding()] | |
param ( | |
[System.Security.Principal.NTAccount]$User, | |
[System.Security.AccessControl.FileSystemRights]$Rights = "FullControl", | |
[System.Security.AccessControl.AccessControlType]$ACLType = "Allow", | |
[Parameter(Mandatory)] | |
[ValidateScript({Test-Path -Path $_ -PathType Container})] | |
$StartingDir | |
) | |
begin { | |
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule( | |
$User, | |
$Rights, | |
$ACLType | |
) | |
} | |
process { | |
foreach ($file in (Get-ChildItem $StartingDir -recurse)) { | |
$acl = Get-Acl -Path $file.FullName | |
#Add this access rule to the ACL | |
$acl.SetAccessRule($rule) | |
#Write the changes to the object | |
Set-Acl $File.Fullname $acl | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment