Managing user assignments in SharePoint Online can be a time-consuming task, especially when dealing with bulk user additions, role assignments, and group memberships. PnP PowerShell simplifies these operations by enabling administrators to efficiently assign users in bulk to SharePoint groups, roles, and sites using PowerShell scripts.
Key Operations Covered
Adding multiple users to SharePoint Groups
Assigning bulk users to site permissions (Owners, Members, Visitors)
Bulk assigning users to SharePoint roles
Removing users in bulk from groups
Automating user assignment from a CSV file
Prerequisites
Before executing PowerShell scripts for bulk user assignments, ensure:
PnP PowerShell is installed
Install-Module -Name PnP.PowerShell -Force -AllowClobber
You are connected to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive
You have the necessary admin permissions (Site Collection Administrator or higher).
Step 1: Creating a CSV File for Bulk User Assignments
To bulk assign users, create a CSV file (UserAssignments.csv
) with the following format:
UserEmail, GroupName, SiteURL, Role
user1@contoso.com, Site Members, https://yourtenant.sharepoint.com/sites/ProjectSite, Edit
user2@contoso.com, Site Owners, https://yourtenant.sharepoint.com/sites/ProjectSite, Full Control
user3@contoso.com, Site Visitors, https://yourtenant.sharepoint.com/sites/ProjectSite, Read
CSV Fields Explained:
- UserEmail: Email of the user to be assigned
- GroupName: The SharePoint group to which the user will be added
- SiteURL: SharePoint site URL where the user will be assigned
- Role: Permission role (Read, Edit, Full Control, etc.)
Step 2: Bulk Assign Users to SharePoint Groups
PnP PowerShell Script to Read CSV and Add Users
Save the following script as BulkUserAssignment.ps1
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive
# Path to CSV file
$csvFilePath = "C:\Path\To\UserAssignments.csv"
# Read CSV file
$users = Import-Csv -Path $csvFilePath
foreach ($user in $users) {
$siteUrl = $user.SiteURL
$groupName = $user.GroupName
$userEmail = $user.UserEmail
$role = $user.Role
Write-Host "Processing user: $userEmail for site: $siteUrl in group: $groupName with role: $role"
# Connect to the specific site
Connect-PnPOnline -Url $siteUrl -Interactive
# Add user to the specified SharePoint Group
Add-PnPUserToGroup -LoginName $userEmail -Group $groupName
Write-Host "Added $userEmail to group: $groupName"
# Assign Role to the User (if applicable)
if ($role -ne "") {
Set-PnPWebPermission -User $userEmail -AddRole $role
Write-Host "Assigned role: $role to $userEmail"
}
}
Write-Host "Bulk User Assignment Completed!" -ForegroundColor Green
How the Script Works:
Reads user details from CSV
Connects to SharePoint Site
Adds the user to the specified SharePoint Group
Assigns permissions (Read, Edit, Full Control)
Step 3: Running the Script
Execute the script in PowerShell:
PowerShell -ExecutionPolicy Bypass -File "C:\Path\To\BulkUserAssignment.ps1"
Successfully assigns users in bulk to SharePoint groups and roles.
Step 4: Bulk Removing Users from SharePoint Groups
If you need to bulk remove users, use this script:
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive
# Read CSV file
$csvFilePath = "C:\Path\To\UserAssignments.csv"
$users = Import-Csv -Path $csvFilePath
foreach ($user in $users) {
$siteUrl = $user.SiteURL
$groupName = $user.GroupName
$userEmail = $user.UserEmail
Write-Host "Removing user: $userEmail from group: $groupName on site: $siteUrl"
# Connect to site
Connect-PnPOnline -Url $siteUrl -Interactive
# Remove User from Group
Remove-PnPUserFromGroup -LoginName $userEmail -Group $groupName
Write-Host "Removed $userEmail from group: $groupName"
}
Write-Host "Bulk User Removal Completed!" -ForegroundColor Green
This script reads the CSV file and removes users from SharePoint Groups.
Step 5: Automating Bulk User Assignment Using Windows Task Scheduler
To automate bulk user assignment, schedule the script in Task Scheduler:
Steps to Automate:
1️⃣ Open Task Scheduler
2️⃣ Click Create Basic Task
3️⃣ Set a daily/weekly schedule
4️⃣ Choose Start a Program and enter:
-ExecutionPolicy Bypass -File "C:\Path\To\BulkUserAssignment.ps1"
5️⃣ Click Finish
Ensures automated user assignments at regular intervals.
Step 6: Verifying User Assignments
After execution, verify assignments using:
List Users in a Group
Get-PnPGroupMembers -Group "Site Members"
List All SharePoint Groups
Get-PnPGroup
Check User Permissions
Get-PnPUser -LoginName "user1@contoso.com"
Ensures all users are correctly assigned.
Troubleshooting Common Issues
1. User Not Added to Group?
Ensure the group name is correct in the CSV.
Check user exists in Microsoft 365.
2. Permission Issues?
Run PowerShell as Administrator.
Ensure you have Site Collection Admin rights.
3. CSV Not Reading Properly?
Open CSV in Notepad to check for extra spaces or special characters.