PowerShell Set permissions on a folder/directory from the command line

Is there a better / easier way to set permissions on a Windows folder from the command line?

I am using powershell, get-acl and set-acl.

# Set permissions on a folder using powershell, get-acl and set-acl.
# make directory before settings permissions. setting permissions code creates file.
# if the user has no permissions on the folder run this script in a window with admin privileges.
# windows 10
# powershell 5
# change zpath and zuser
$zpath="F:\Users\Default\Documents\_NOTEPAD++ AUTOBACKUP"
$zuser="_9doug"
If(!(test-path $zpath)){New-Item -ItemType directory -Path $zpath | Out-Null}
$Acl = Get-Acl $zpath
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($zuser, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$Acl.SetAccessRule($Ar)
Set-Acl $zpath $Acl
exit 
6

1 Answer

Windows 10 64-bit.

Use icacls. Does not require admin privileges if the user does not exist on the object.

Grant User Full Permissions:

icacls "%userprofile%\Documents\021219" /inheritance:d /grant:r %username%:(OI)(CI)F /T

Remove a user:

icacls "%userprofile%\Documents\021219" /remove %UserName%

Help:

icalcs /?

icacls command line options

Grant user full permissions from the command line.

Remove user from the command line.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like