none
Set Folder Permission by Powershell

    Question

  • Hi,

    I need to set folder level permissions for a document library that has around 140 folders, so im thinking on doing this by powershell, but im having problems getting the folders by name so i can set specific folder permissions.

    How can i select a specific folder on a document library by name and set its permissions?

    Thanks,

    Thursday, October 20, 2011 8:49 AM

Answers

  • Hi,

    You can do this in a simple console application.

    eg:

    SPSite MySite = new SPSite("http://adfsaccount:2222/");

     SPWeb MyWeb = MySite.OpenWeb();

     

     SPDocumentLibrary MyDocLibrary = (SPDocumentLibrary) MyWeb.Lists["My Documents"];

    SPFolder folder = MyDocLibrary.RootFolder;

    foreach(SPFolder fld in folder.SubFolders)

    {

       if (fld.Name.Equals("xyz", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // set permission rights on the folder
                    }

    }

    I hope this will help you

     

    Thanks,

    Amit

    • Proposed as answer by Ravi S Kulkarni Thursday, October 20, 2011 10:14 AM
    • Marked as answer by Shimin Huang Thursday, October 27, 2011 8:13 AM
    Thursday, October 20, 2011 9:42 AM
  • Hi

     

    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
    
    

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

    Hope the above code helpful to your question. 


    Regards

    Satish Kumar Raja.

    ---------------------------------------------------

    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.

    • Edited by Satish05 Friday, October 21, 2011 7:08 AM
    • Marked as answer by Shimin Huang Thursday, October 27, 2011 8:13 AM
    Friday, October 21, 2011 7:07 AM

All replies

  • visit the link Set Folder Permissions using a PowerShell script

    hope this helps.


    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Thursday, October 20, 2011 9:33 AM
  • Hi,

    You can do this in a simple console application.

    eg:

    SPSite MySite = new SPSite("http://adfsaccount:2222/");

     SPWeb MyWeb = MySite.OpenWeb();

     

     SPDocumentLibrary MyDocLibrary = (SPDocumentLibrary) MyWeb.Lists["My Documents"];

    SPFolder folder = MyDocLibrary.RootFolder;

    foreach(SPFolder fld in folder.SubFolders)

    {

       if (fld.Name.Equals("xyz", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // set permission rights on the folder
                    }

    }

    I hope this will help you

     

    Thanks,

    Amit

    • Proposed as answer by Ravi S Kulkarni Thursday, October 20, 2011 10:14 AM
    • Marked as answer by Shimin Huang Thursday, October 27, 2011 8:13 AM
    Thursday, October 20, 2011 9:42 AM
  • Hi

     

    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
    
    

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

    Hope the above code helpful to your question. 


    Regards

    Satish Kumar Raja.

    ---------------------------------------------------

    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.

    • Edited by Satish05 Friday, October 21, 2011 7:08 AM
    • Marked as answer by Shimin Huang Thursday, October 27, 2011 8:13 AM
    Friday, October 21, 2011 7:07 AM