Setting file ACLs with PowerShell part 5

Security Briefs

Syndication

In my previous post in this series, I showed how easy it is to work with enumerations in PowerShell. Here's the code I've been walking through in this series:

$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)

All I've got left to explain is the last line of code. It's important to note that when you're tweaking ACLs, there are basically three steps you have to take:

  1. Read the ACL from an object.
  2. Tweak it however you like.
  3. Write the ACL back to the object.

In this last line of code, I'm applying the new, tweaked ACL back to the file foo.txt. At this point the new access control policy takes effect.

Navigate posts in this series: prev


Posted Jan 09 2008, 04:53 AM by keith-brown