Set Permissions on a File or Directory using PowerShell

Set Permissions on a File or Directory using PowerShell
There are basically two commands which are used to play around with permissions on a fileGet-Acl – The Get-Acl cmdlet gets objects that represent the security descriptor of a file or resource. The security descriptor contains the access control lists (ACLs) of the resource.

The ACL specifies the permissions that users and user groups have to access the resource.

Set-Acl – The Set-Acl cmdlet changes the security descriptor of a specified item, such as a file or a registry key, to match the values in a security descriptor that you supply.

To use Set-Acl, use the Path or InputObject parameter to identify the item whose security descriptor you want to change.

Then, use the AclObject or SecurityDescriptor parameters to supply a security descriptor that has the values you want to apply.

Set-Acl applies the security descriptor that is supplied. It uses the value of the AclObject parameter as a model and changes the values in the item’s security descriptor to match the values in the AclObject parameter.

These Commands are a part of Microsoft.PowerShell.Security PSSnapin

Here im showing an example to change permissions on a folder data placed on my c: drive.

First lets see the current permissions on the folder

Get-Acl C:data

 

<#To change the ACL of a file, use the Set-Acl cmdlet. This example prevents the

TempUser Account from write-permissions on a folder:#>
 
$acl = Get-Acl C:data
 
$arguments = "Hyper-V12-5TempUser","Write","Deny"
 
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $arguments
 
$acl.SetAccessRule($accessRule)
 

$acl | Set-Acl C:data

 

<#Now if i again check i see that the folder permissions have been changed
and TempUser Account has been denied to write in a file #>

Get-Acl C:data

 

Share this post

5 thoughts on “Set Permissions on a File or Directory using PowerShell

  1. I see you don’t monetize your site, don’t waste your traffic, you can earn extra cash every month because you’ve got high quality content.
    If you want to know how to make extra money, search for: Ercannou’s essential adsense alternative

    Reply

Post Comment