Collaborate, Innovate, Automate

Add Group Members

This PnP PowerShell script automates the process of adding users to SharePoint Online sites with appropriate permission levels. It streamlines user provisioning and ensures consistent access management across your SharePoint environment.

Purpose

This script helps with SharePoint user management by:

Prerequisites

PowerShell Script

# Script 2: Add users to existing SharePoint permission groups

            param(
                [string]$SiteUrl = "https://tenantName.sharepoint.com/sites/siteName",
                [string]$ClientId = "",
                [string]$GroupName = "" # Define the group name
            )
            
            # Define the list of users to add to the group (modify these email addresses)
            $UserEmails = @(
                "user@tenant.com"
            )
            
            try {
                # Connect to SharePoint Online site
                Write-Host "Connecting to SharePoint site: $SiteUrl" -ForegroundColor Green
                Connect-PnPOnline -Url $SiteUrl -Interactive -ClientId $ClientId
                
                # Check if group already exists
                $existingGroup = Get-PnPGroup -Identity $GroupName -ErrorAction SilentlyContinue
                
                if (-not $existingGroup) {
                    Write-Host "Error: Group '$GroupName' doesn't exist." -ForegroundColor Red
                    return
                }
                
                Write-Host "Found existing group: $GroupName" -ForegroundColor Green
                
                # Add users to the group
                Write-Host "Adding users to group: $GroupName" -ForegroundColor Green
                $successCount = 0
                $failCount = 0
                
                foreach ($userEmail in $UserEmails) {
                    try {
                        Write-Host "Processing $userEmail..." -ForegroundColor Yellow
                        
                        # Get the group members to check if user already exists
                        $GroupMembers = Get-PnPGroupMember -Identity $GroupName | Select-Object -ExpandProperty Email
                        
                        # Check if the user is already a member
                        if ($GroupMembers -contains $userEmail) {
                            Write-Host "⚠ $userEmail is already a member of $GroupName" -ForegroundColor Yellow
                            continue
                        } else {
                            Write-Host "$userEmail is not a member of $GroupName. Adding..." -ForegroundColor Yellow
                            # Add the user to the group using Add-PnPGroupMember
                            Add-PnPGroupMember -LoginName $userEmail -Identity $GroupName
                            Write-Host "✓ Added $userEmail to $GroupName" -ForegroundColor Green
                            $successCount++
                        }
                        
                    } catch {
                        Write-Host "✗ Failed to add $userEmail to $GroupName - Error: $($_.Exception.Message)" -ForegroundColor Red
                        $failCount++
                    }
                }
                
                # Summary
                Write-Host "`n--- Summary ---" -ForegroundColor Cyan
                Write-Host "Successfully added: $successCount users" -ForegroundColor Green
                Write-Host "Failed to add: $failCount users" -ForegroundColor Red
                
                # Display final group members
                Write-Host "`nFinal members of '$GroupName':" -ForegroundColor Cyan
                $finalMembers = Get-PnPGroupMember -Identity $GroupName
                foreach ($member in $finalMembers) {
                    Write-Host "  - $($member.Email) ($($member.Title))" -ForegroundColor White
                }
                
            } catch {
                Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
            } finally {
                Disconnect-PnPOnline
            }
            

Usage Notes