Skip to content

Instantly share code, notes, and snippets.

@r-plus
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save r-plus/a59f1c73f62bdeea0c23 to your computer and use it in GitHub Desktop.

Select an option

Save r-plus/a59f1c73f62bdeea0c23 to your computer and use it in GitHub Desktop.
Add everyone read-only ACE to target path.
# Add everyone read-only ACE to target path.
# Doc: https://technet.microsoft.com/en-us/library/ff730951.aspx
function Add-ReadOnlyACE([string]$path)
{
if (-not (Test-Path $path)) { return }
# Create additionnal ACE.
$colRights = [System.Security.AccessControl.FileSystemRights]"AppendData, ChangePermissions, CreateDirectories, CreateFiles, Delete, DeleteSubdirectoriesAndFiles, Write"
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$objType =[System.Security.AccessControl.AccessControlType]::Deny
$objUser = New-Object System.Security.Principal.NTAccount("Everyone")
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
# Get then add ACE.
$objACL = Get-Acl $path
$objACL.AddAccessRule($objACE)
Set-Acl $path -AclObject $objACL
}
Add-ReadOnlyACE "C:\test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment