1

How (which command) can I use to grant permissions (full_control) to a user (service_account) on a folder using PowerShell?

Thank you!

I was trying to use icacls in my PowerShell but it's not working.

vonPryz
  • 22,996
  • 7
  • 54
  • 65
Lana
  • 11
  • 1
  • 2
  • 1
    Hi, welcome to StackOverflow, here you can find a guide to create the perfect question: https://stackoverflow.com/tour. Can you provide an example of what have you done with powershell? – Ema.jar Jan 24 '19 at 22:33
  • This might helpful: https://stackoverflow.com/questions/25779423/powershell-to-set-folder-permissions – CR241 Jan 24 '19 at 22:36

2 Answers2

5

There are several ways to do this. If you don't want to install a module as James suggests above then:

# Get current access permissions from folder and store in object
$Access = Get-Acl -Path $FolderPath
# Create new object with required new permissions
$NewRule = New-Object System.Security.AccessControl.FileSystemAccessRule("MyDomain\MyUserOrGroup","FullControl","ContainerInherit,ObjectInherit","None","Allow")
# Add new rule to our copy of the current rules
$Access.AddAccessRule($NewRule)
# Apply our new rule object to destination folder
Set-Acl -Path $FolderPath -AclObject $Access -ErrorAction Stop

As James mentions though, using ACLs in Powershell without a module, whilst powerful, is also a pain. I only do it when I'm sharing scripts (so that there isn't a dependency on the module).

Scepticalist
  • 3,737
  • 1
  • 13
  • 30
1

I would recommend using the NTFSSecurity Powershell module for setting the permissions as it's much easier to use (and understand) than acls!

To add permissions for a user is just one command.

I've shown two examples for both network paths/domain account and local folder/local user. They can be mixed in any way you can set via the GUI.

Add-NTFSAccess -Path "\\sdk\data\SHAREDIR\$NAME" -Account "Domain\K_NV_TEST_R" -AccessRights FullControl

Add-NTFSAccess -Path "C:\folder\subfolder" -Account "LocalComputerName\LocalUserName" -AccessRights FullControl
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40