Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PowerShell: Grant NTFS Permissions to Folders

Posted on 2016-08-11
4
Medium Priority
?
97 Views
Last Modified: 2016-08-11
I have a folder structure in a domain environment that contains child folders bearing the names of the Active Directory user accounts that i need to have full control to their respective folder.  How would i script this permission change in PowerShell?
0
Comment
Question by:marrj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Schnell Solutions
ID: 41752490
Let's assume that your folder is located at 'C:\Public'

In order to use power shell ACL modification commands and avoid specifying many lines of code, it is better that you use a variable containing the reference permissions that you need. For example, let's say that you have the file 'C:\Public\ACLTemplate.txt' including the NTFS permissions that you have. You can take these permissions as a reference and put then in a variable like this:
$DesiredPermissions = Get-ACL C:\Public\ACLTemplate.txt

Finally, in order to apply all of these NTFS settings to all existing folders in C:\Public. (i.e. C:\Public\Folder1, C:\Public\Folder2, C:\Public\Folder3, ... , C:\Public\FolderN
You can execute:

Set-ACL   -Path C:\Public\*   -AclObject   $DesiredPermissions


Or if you want, you can make the whole process with just one line, and no need of a variable just using:
Get-ACL C:\Public\ACLTemplate.txt | Set-ACL   -Path C:\Public\*
0
 
LVL 14

Expert Comment

by:Schnell Solutions
ID: 41752523
By the way, I believe that you know that you can also use CMD commands, even from Power Shell in order to achieve the same goal. Using ICACLS for example, allows you to specify the desired permissions easily.

For example, if you want to assign Full access permission for John.Smith in all the sub-folders inside C:\Public, you just need to run:

ICACLS   C:\Public\*   /Grant   john.smith:F
0
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41752599
If the folders match the usernames, you can use the following.
$path = "C:\Share"

$folders = Get-ChildItem -Path $path

foreach ($folder in $folders)
{
    try {
    $user = $folder.BaseName
    $acl = Get-Acl $folder.FullName
    $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","Allow")
    $acl.SetAccessRule($ar)
    Set-Acl $($folder.FullName) $acl
    } catch {
    Write-Host $folder.FullName did not have a matching user.
    }
}

Open in new window

0
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 2000 total points
ID: 41752612
I should mention, you can test run this with this code (added -whatif to line 12):
$path = "C:\Share"

$folders = Get-ChildItem -Path $path

foreach ($folder in $folders)
{
    try {
    $user = $folder.BaseName
    $acl = Get-Acl $folder.FullName
    $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","Allow")
    $acl.SetAccessRule($ar)
    Set-Acl $($folder.FullName) $acl -whatif
    } catch {
    Write-Host $folder.FullName did not have a matching user.
    }
}

Open in new window

0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question