Search code examples
windowspowershellcmd

Using icacls to set correct permissions for all app_data folders on drive


I'm in the process of setting up a new server from a backup of a load of different websites. Copying across the backup has lost all the permissions for each file/folder, so I need to set the correct permissions. I know I can use icacls to do this manually, but I figured it'd be quicker if I was able to run a script that finds all the app_data folders on the new drive and recursively sets the file permissions for IIS_IUSRS.

I'm not sure where to start on this. Can anyone help? TIA.


Solution

  • Options that come to mind:

    1. Robocopy's /Copy Parameter which has an s option for security.
    2. NTFSSecurity Module, obviously conceptually similar to icalcs. For obvious reasons this is my first choice for dealing with NTFS security.
    3. Get/Set-Acl. Anything beyond the most basic work usually had me resorting to .Net classes. Don't get me wrong, PowerShell's great at that, but for something as central to Windows as NTFS ACLs I always expected something more native. Ergo the NTFSSecurity Module. That said, in this case, where you simply want to copy an ACL it may be as simple as something like below:
    $ACLModel = Get-Acl -Path <SourceFolder>
    Set-Acl -Path <DestFolder> -AclObject $ACLModel