Using ICACLS to Secure Folders

by Jan 3, 2014

Console applications are equal citizens in the PowerShell ecosystem. In this example, a function uses icacls.exe to secure a newly created folder:

function New-Folder 
{
  param
  (
    $Path, 
    
    $Username
  )


  If ( (Test-Path -Path $path) -eq $false ) 
  {
    New-Item $path -Type Directory | Out-Null
  }
    
  icacls $path /inheritance:r /grant '*S-1-5-32-544:(OI)(CI)R' ('{0}:(OI)(CI)F' -f $username)
} 

The function New-Folder will create a new folder (if it does not exist) and then use icacls.exe to turn off inheritance and grant read permissions to the Administrators group and full permissions to the user specified.

Twitter This Tip! ReTweet this Tip!