I would like to do the equivalent of chmod -w+r-x foo
or attrib +R foo
in Windows Powershell. Putzing around, I notice a fairly gnarly Set-Acl
function that looks significantly fancier than what I need.
How do I do attrib +R foo
in Windows Powershell?
How to change files and folders attributes using PowerShell? There are multiple files and folders attribute supported by the Windows Operating System. To check which attributes that files and folders support use DOS command attrib /? You can see the attributes listed like Read-Only, Archive, etc. You can set the attribute using PowerShell.
The first PowerShell command used to manage file and folder permissions is Get-Acl; it lists all object permissions. A user must own both the target and source folders to copy permissions.
When defining permissions for the Windows registry with PowerShell, you’ll need to create a System.Security.AccessControl.RegistryAccessRule object. This object allows you to define criteria like the principal (user, group, etc.) that this ACE applies to, level of access, and if you’re going to allow or deny that access.
The hierarchy for permissions is as follows: The first PowerShell command used to manage file and folder permissions is Get-Acl; it lists all object permissions. A user must own both the target and source folders to copy permissions.
The PowerShell Community Extensions comes with Set-Writable (aliased to swr) and Set-ReadOnly (aliased to sro). I use them both frequently. Removing execute privileges does require a change to the ACLs however using Get-Acl/Set-Acl is painful. I would use icacls.exe.
BTW if you don't want to mess with any 3rd party stuff, setting readonly to true/false is pretty easy:
Set-ItemProperty foo.txt IsReadOnly $true # or $false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With