Automating Folder Creation with Permissions Based on CSV in SharePoint Online

Aran Billen 941 Reputation points
2025-01-28T13:56:05.6833333+00:00

Hi everyone,

I’m reaching out because I need assistance in automating the creation of multiple folders in SharePoint Online which is part of a team called Staff training. Here’s what I’m working with:

  • Goal: I need to create approximately 100 folders with unique names (these names are provided in a CSV file).
  • Folder Structure: The folders should be created in a specific location: Staff > Work Experience > [Folder Name].
  • Permissions: Each folder will require specific users (around 6 per folder) to have edit access to it, as listed in the CSV.
  • Task: Manually creating each folder and assigning permissions would be very time-consuming, so I’m looking for a script or solution that can automate this task by referencing the data in the CSV file.

Could anyone help me with a PowerShell script or another method to automate the creation of folders and user permissions based on a CSV in SharePoint Online? I’d greatly appreciate any help you can provide!

Thanks in advance!

Microsoft 365 and Office Install, redeem, activate For business Windows
Microsoft 365 and Office SharePoint Development
Windows for business Windows Server User experience PowerShell
Microsoft Teams Microsoft Teams for business Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cummins Glen 0 Reputation points
    2025-01-28T15:44:05.3166667+00:00

    You're right—manually creating 100 folders and assigning permissions would be very tedious. Fortunately, PowerShell can help automate this process efficiently using the SharePoint Online Management Shell or the PnP PowerShell module.

    Here’s a general outline and PowerShell script using the PnP PowerShell module to automate the creation of folders and assign permissions based on a CSV file.

    Prerequisites:

    1. Install PnP PowerShell: If you haven’t already installed the PnP PowerShell module, you can do so by running the following command:

    2.  Install-Module -Name "PnP.PowerShell" -Force -AllowClobber

    1. CSV File Structure: Your CSV should have the following columns:
      • FolderName: The name of the folder to be created.
        • UserEmails: A comma-separated list of users to be given edit permissions.

    Example:

    FolderName,UserEmails

    Folder1,email1@example.com,email2@example.com

    Folder2,email3@example.com,email4@example.com

    PowerShell Script:

    Connect to SharePoint Online

    $siteUrl = "https://yourtenant.sharepoint.com/sites/StaffTraining"

    Connect-PnPOnline -Url $siteUrl -Interactive

     

    Path to the CSV file

    $csvPath = "C:\Path\To\Your\CSV\File.csv"

     

    Loop through each row in the CSV file

    Import-Csv -Path $csvPath | ForEach-Object {

        $folderName = $_.FolderName

        $userEmails = $_.UserEmails -split ','

     

        # Create the folder inside the specified document library

        $libraryPath = "Staff/Work Experience"

        $folderPath = "$libraryPath/$folderName"

     

        # Create the folder if it doesn't exist

        New-PnPFolder -Name $folderName -Folder $libraryPath

     

        # Set permissions for each user

        foreach ($email in $userEmails) {

            $user = Get-PnPUser -Identity $email

     

            if ($user) {

                # Break inheritance and assign edit permissions

                Set-PnPFolderPermission -Identity $folderPath -User $user.LoginName -AddRole "Edit"

            }

            else {

                Write-Warning "User $email not found in SharePoint site."

            }

        }

    }

     

    Disconnect from SharePoint Online

    Disconnect-PnPOnline

    How It Works:

    1. Connect to SharePoint Online: The script connects to your SharePoint Online site.
    2. Read CSV: The script imports the CSV file that contains folder names and the list of users for each folder.
    3. Create Folders: For each row in the CSV, it creates a new folder inside the “Staff > Work Experience” directory.
    4. Assign Permissions: It assigns the specified users as "Edit" permissions for each folder.
    5. Error Handling: If a user is not found, a warning is logged.

    Notes:

    • You might need to adjust $siteUrl and $csvPath to match your environment.
    • The script assumes that the users already exist in the SharePoint site. If not, you would need to add a check or a user creation step, which could be complex as SharePoint Online doesn't have a native way to add external users via PowerShell easily.
    • Be careful with permission management to ensure you don’t inadvertently modify any existing permissions.
    • If you have other specific permission settings, such as if the users should have a different permission level (like “Contribute” or “View”), you can replace -AddRole "Edit" with other roles such as "Contribute" or "View".

    Testing:

    Make sure to test the script with a smaller dataset first (e.g., 1 or 2 folders) to ensure everything works as expected before running the full script.

    Let me know if you run into any issues or need further assistance!

     

    You're right—manually creating 100 folders and assigning permissions would be very tedious. Fortunately, PowerShell can help automate this process efficiently using the SharePoint Online Management Shell or the PnP PowerShell module.

    Here’s a general outline and PowerShell script using the PnP PowerShell module to automate the creation of folders and assign permissions based on a CSV file.

    Prerequisites:

    1. Install PnP PowerShell: If you haven’t already installed the PnP PowerShell module, you can do so by running the following command:

    2.  Install-Module -Name "PnP.PowerShell" -Force -AllowClobber

    1. CSV File Structure: Your CSV should have the following columns:
      • FolderName: The name of the folder to be created.
        • UserEmails: A comma-separated list of users to be given edit permissions.

    Example:

    FolderName,UserEmails

    Folder1,email1@example.com,email2@example.com

    Folder2,email3@example.com,email4@example.com

    PowerShell Script:

    Connect to SharePoint Online

    $siteUrl = "https://yourtenant.sharepoint.com/sites/StaffTraining"

    Connect-PnPOnline -Url $siteUrl -Interactive

     

    Path to the CSV file

    $csvPath = "C:\Path\To\Your\CSV\File.csv"

     

    Loop through each row in the CSV file

    Import-Csv -Path $csvPath | ForEach-Object {

        $folderName = $_.FolderName

        $userEmails = $_.UserEmails -split ','

     

        # Create the folder inside the specified document library

        $libraryPath = "Staff/Work Experience"

        $folderPath = "$libraryPath/$folderName"

     

        # Create the folder if it doesn't exist

        New-PnPFolder -Name $folderName -Folder $libraryPath

     

        # Set permissions for each user

        foreach ($email in $userEmails) {

            $user = Get-PnPUser -Identity $email

     

            if ($user) {

                # Break inheritance and assign edit permissions

                Set-PnPFolderPermission -Identity $folderPath -User $user.LoginName -AddRole "Edit"

            }

            else {

                Write-Warning "User $email not found in SharePoint site."

            }

        }

    }

     

    Disconnect from SharePoint Online

    Disconnect-PnPOnline

    How It Works:

    1. Connect to SharePoint Online: The script connects to your SharePoint Online site.
    2. Read CSV: The script imports the CSV file that contains folder names and the list of users for each folder.
    3. Create Folders: For each row in the CSV, it creates a new folder inside the “Staff > Work Experience” directory.
    4. Assign Permissions: It assigns the specified users as "Edit" permissions for each folder.
    5. Error Handling: If a user is not found, a warning is logged.

    Notes:

    • You might need to adjust $siteUrl and $csvPath to match your environment.
    • The script assumes that the users already exist in the SharePoint site. If not, you would need to add a check or a user creation step, which could be complex as SharePoint Online doesn't have a native way to add external users via PowerShell easily.
    • Be careful with permission management to ensure you don’t inadvertently modify any existing permissions.
    • If you have other specific permission settings, such as if the users should have a different permission level (like “Contribute” or “View”), you can replace -AddRole "Edit" with other roles such as "Contribute" or "View".

    Testing:

    Make sure to test the script with a smaller dataset first (e.g., 1 or 2 folders) to ensure everything works as expected before running the full script.

    Let me know if you run into any issues or need further assistance!

     


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.