Solved

PowerShell: Grant NTFS Permissions to Folders

Posted on 2016-08-11
4
34 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
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Schnell Solutions
Comment Utility
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
Comment Utility
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 12

Expert Comment

by:Dustin Saunders
Comment Utility
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 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
Comment Utility
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

How does your email signature look on mobiles?

Do your employees use mobile devices to reply to emails? With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
OfficeMate Freezes on login or does not load after login credentials are input.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now