Home > Client Management > Set Folder Permissions using a PowerShell script

Set Folder Permissions using a PowerShell script

A common Admin task is Setting permissions on folders for new Users or Groups. doing this manually can be pretty boring and timeconsuming. This script automates these steps through PowerShell.

The parameters that I’ve added to the script are:

  • -Path Folder to Create (Required)
  • -User User who should have access (Required)
  • -Permission Specify Permission for User, Default set to Modify (Optional)
  • -help Prints the HelpFile (Optional)

The script sets the folderpermissions for a User or a group on a folder and if the folder doesn’t exist, it creates the folder and adds the specified permissions.

Running the Script on one folder gives the user or group permissions on the folder and on child folders. If you run the script recurse, it will break the inheritance for the specified User/Group and set the permissions specified on each folder.

Here are 2 examples on running the script.


./SetFolderPermission.ps1 -path C:\User -Access APA\MyGroup -Permission Write

Get-ChildItem -path C:\User -recurse |
Where { $_.Attributes -match "d"} |
ForEach {
./SetFolderPermission.ps1 -path $_.Fullname -Access APA\MyGroup -Permission Read
}

If you want to display the HelpText simply type:


./SetFolderPermission.ps1 -help

Here’s a link to the script

Rating 3.00 out of 5
[?]
  1. Todd
    April 7th, 2009 at 20:23 | #1

    nice script it does not appear to be able to st permissions on files though (only folders)

  2. April 8th, 2009 at 16:10 | #2

    Hi Todd.

    Yes, the script only sets permissions on folder level, if there are files within the folder they should inherit the permissions from the folder.

  3. April 16th, 2009 at 05:30 | #3

    hm. strange ))

  4. Josh
    May 15th, 2009 at 18:17 | #4

    Niklas,
    Very nice script. Do you know if there is a way to modify the script to set a maximum security level? For example, if I have a user with FullControl, and I want to modify the user’s permissions to (only) ReadandExecute.

  5. Josh
    May 15th, 2009 at 19:42 | #5

    Please disregard my last post. I see that your script already does this. I was attempting to modify the permissions for the users group. But, since the users group is actually ‘BUILTIN\Users’, the $_.IdentityReference was never equal to $Access. So, it was jumping to $GetACL.AddAccessRule($AccessRule). Once I figued this out, it works like a charm. Thanks for the great script!

  6. May 18th, 2009 at 20:40 | #6

    I’m glad you liked it!

    I found a great post on Copying ACL:s written by Jeffrey Snover.
    It’s worth a look.

    http://blogs.msdn.com/powershell/archive/2009/05/11/copy-acl.aspx

    Regards Niklas

  7. May 28th, 2009 at 20:46 | #7

    Niklas,

    I just started working with PowerShell …. I can’t believe i waited this long and kept doing things with VBScript! anyway ….. I tested your script and works nicely, good job.

    I have several questions:

    a) If i wanted to run the script remotely to create individual folders based on a user’s list the script is reading (I extracted it from AD) to create individual user folders within an existing or non-existing share on a remote server, what would it be the best way? in the past using VBScript this what I have done:

    Option Explicit
    Dim intRow, objExcel, objSheet, strPathExcel
    Dim strHomeFolder, strHome, strUser
    Dim objFSO, objShell, intRunError

    strHome = “\\ServerName\ShareName\”
    strPathExcel = “C:\ExcelFile.xlsx”
    intRow = 3 ‘ Row 1 contains headings
    ‘ Open the Excel spreadsheet
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objExcel = CreateObject(“Excel.Application”)
    Set objSheet = objExcel.Workbooks.Open(strPathExcel)
    ‘ Create a shell for cmd and CACLS
    Set objShell = CreateObject(“Wscript.Shell”)

    ‘ Here is the loop that cycles through the cells
    Do Until (objExcel.Cells(intRow,1).Value) = “”
    strUser = objExcel.Cells(intRow, 1).Value
    Call HomeDir ‘ I decided to use a subroutine
    intRow = intRow + 1
    Loop
    objExcel.Quit ‘ Clears up Excel

    Sub HomeDir()
    strHomeFolder = strHome & strUser
    If strHomeFolder “” Then
    If Not objFSO.FolderExists(strHomeFolder) Then
    On Error Resume Next
    objFSO.CreateFolder strHomeFolder
    If Err.Number 0 Then
    On Error GoTo 0
    Wscript.Echo “Cannot create: ” & strHomeFolder
    End If
    On Error GoTo 0
    End If
    If objFSO.FolderExists(strHomeFolder) Then

    ‘ Assign user permission to home folder.
    intRunError = objShell.Run(“%COMSPEC% /c Echo Y| cacls “_
    & strHomeFolder & ” /e /c /g Administrators:f “_
    & strUser & “:F”, 2, True)
    If intRunError 0 Then
    Wscript.Echo “Error assigning permissions for user ” & strUser & ” to home folder ” &
    strHomeFolder
    End If
    End If
    End If
    End Sub
    objExcel.Quit
    WScript.Quit

    The previous script works and I do want to emulate the same with your script.

    b) is there a better way to do this such as pulling the data directly from AD? using som type of AD query or Wmi calling?

  8. May 29th, 2009 at 14:52 | #8

    I guess this posting you have in your blog answers my previous question:

    http://www.powershell.nu/2009/04/27/part-116-adding-homefolder-through-powershell/

    (I found it after I wrote this ….)

    Anyway, I am having anissue with your Get-AD.ps1 file …

  9. May 31st, 2009 at 20:22 | #9

    Hi.

    Can you describe the Get-AD.ps1 issue?

    Regards

    Niklas

  10. January 11th, 2010 at 19:01 | #10

    Sweet simple working one buddy !! Thanks !!

  11. January 11th, 2010 at 19:01 | #11

    useful script. thank you

  12. January 20th, 2010 at 19:08 | #12

    Awesome. It works fine for remote computer too.

  1. No trackbacks yet.

Spam Protection by WP-SpamFree