Setting file ACLs with PowerShell

Security Briefs

Syndication

2007 marked the year that I switched shells. I used to use the Hamilton C Shell, which was powerful and integrates exceptionally well with Windows, but I found myself constantly having to relearn the script syntax whenever I'd want to do something. So when PowerShell came along, I bought Bruce's book and went to town with it. There are so many things I love about PowerShell that it'll take many blog entries to cover them all.

One of the problems I solved that I've had on my list of "blog topics" for ages was how to set ACLs on files using PowerShell. Below is a *very* basic script that will get you started. This grants a user named "keith" permission to modify a file called "foo.txt" in the current directory. It's the simplest example I can think of that demonstrates the idea. I'll explore this in future posts and eventually translate it into a more universal script that you can use in production, but this should get you thinking!

$dacl = (dir foo.txt).GetAccessControl()
$newRule = New-Object Security.AccessControl.FileSystemAccessRule "keith",
Modify, Allow
$modified = $false
$dacl.ModifyAccessRule("Add", $newRule, [ref]$modified)
(dir foo.txt).SetAccessControl($dacl)

Navigate posts in this series: next


Posted Oct 23 2007, 08:29 AM by keith-brown

Comments

Lee Holmes wrote re: Setting file ACLs with PowerShell
on 10-31-2007 7:30 AM
Great posts. In addition to the straight .NET syntax, PowerShell also offers the Get-Acl and Set-Acl cmdlets. They work on any providers (such as the FileSystem and Registry) that support ACL operations.

Lee
Michael wrote re: Setting file ACLs with PowerShell
on 05-23-2008 9:32 AM
Is it possible to use the RegistryKey.OpenRemoteBaseKey Method, RegistryKey.GetAccessControl Method and the
RegistryKey.SetAccessControl Method to manipulate a remote computer's registry's ACL? I am able to change the local registry ACL's. I am also able to add and remove keys and values of remote machines but am unsuccessful in my attempts to combine the two.



Keith Brown wrote re: Setting file ACLs with PowerShell
on 05-23-2008 10:11 AM
It's logically a bit tricky to set ACLs on remote machines since SIDs are formed differently on different machines (for example, local accounts have the machine SID as a prefix). So it doesn't surprise me if this wasn't supported, although I've not personally tried to do this.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?