Powershell 3.0 - Add Group/Username and Enable Full Control permissions

sj77
sj77 used Ask the Experts™
on
I am needing a script to add in a user. Let's say everyone into the security properties of a directory.

Directory: C:\CBASYNC
User: Everyone
Permissions needed: Full Control

How would I write this out in powershell?
ps_properties.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:
This creates and adds the access control entry.
$acl = Get-Acl "C:\CBASYNC"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
    'Everyone',
    'FullControl',
    'ObjectInherit,ContainerInherit',
    'None',
    'Allow'
)
$acl.AddAccessRule($accessRule)
Set-Acl "C:\CBASYNC" -AclObject $acl

Open in new window

PowerShell Developer
Top Expert 2010
Commented:
Just a word on the other permission levels.

Get-Acl and Set-Acl do very little for us here, they provide access to the ACL, nothing more. The FileSystemAccessRule is being given strict values.

The access rule is a .NET class: https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemaccessrule(v=vs.110).aspx

It has strictly defined values.
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
    'Everyone',    # An identity (domain\user), or a well known principal (like Everyone)
    'FullControl',    # Access rights
    'ObjectInherit,ContainerInherit',    # Inheritance flags
    'None',    # Propagation flags
    'Allow'    # Type
)

Open in new window

Each of these has an associate list of constants:

Access rights: https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights(v=vs.110).aspx
Inheritance flags: https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.inheritanceflags(v=vs.110).aspx
Propagation flags: https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.propagationflags(v=vs.110).aspx
Type: https://msdn.microsoft.com/en-us/library/w4ds5h86(v=vs.110).aspx

FullControl is a very broad permission, an easy one to grant.

If you wanted to grant Read only (ReadAndExecute) or Modify you would need to combine those rights with Synchronize. This allows them to appear properly without clicking on the Advanced button in folder properties. For example, this grants the Modify right:
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
    'Everyone',
    'ReadAndExecute,Synchronize',
    'ObjectInherit,ContainerInherit',
    'None',
    'Allow'
)

Open in new window

You can be as granular as you wish with the rights. It can always be written as a comma delimited list as above. PowerShell converts that into numeric values in the background.

These values can be modified such that you can build any possible right you might assign in the GUI.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial