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:
- 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
- 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.
- FolderName: The name of the folder to be created.
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:
- Connect to SharePoint Online: The script connects to your SharePoint Online site.
- Read CSV: The script imports the CSV file that contains folder names and the list of users for each folder.
- Create Folders: For each row in the CSV, it creates a new folder inside the “Staff > Work Experience” directory.
- Assign Permissions: It assigns the specified users as "Edit" permissions for each folder.
- 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:
- 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
- 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.
- FolderName: The name of the folder to be created.
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:
- Connect to SharePoint Online: The script connects to your SharePoint Online site.
- Read CSV: The script imports the CSV file that contains folder names and the list of users for each folder.
- Create Folders: For each row in the CSV, it creates a new folder inside the “Staff > Work Experience” directory.
- Assign Permissions: It assigns the specified users as "Edit" permissions for each folder.
- 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!