How to Add Users to SharePoint Group with PowerShell


Managing permissions and group memberships in SharePoint is critical for effective collaboration and security. Automating the process of adding users to SharePoint groups using PowerShell can save time and reduce manual errors. In this blog, we’ll explore how to do this step by step, with practical use cases and sample scripts.


Prerequisites

Before diving into the scripts, ensure you have the following:

  • SharePoint Management Shell installed on your system.
  • Appropriate administrative permissions for the target SharePoint site.
  • PnP PowerShell Module installed. You can install it via PowerShell
Install-Module -Name PnP.PowerShell
  • Access to the SharePoint Online or On-Premises environment.

Step-by-Step Guide
  • Connect to SharePoint

To interact with SharePoint, establish a connection using PnP PowerShell.

For SharePoint Online:

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -UseWebLogin

For SharePoint On-Premises:

Connect-PnPOnline -Url "http://yourserver/sites/YourSite" -Credentials (Get-Credential)
  • Add a Single User to a Group

Here’s a script to add one user to a specific group:

# Variables
$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$groupName = "Site Members"
$userEmail = "user@domain.com"

# Connect to SharePoint
Connect-PnPOnline -Url $siteUrl -UseWebLogin

# Add user to the group
Add-PnPUserToGroup -Group $groupName -LoginName $userEmail

Write-Host "User $userEmail added to $groupName successfully."
  • Add Multiple Users to a Group

To bulk-add users, use the following script:

# Variables
$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$groupName = "Site Members"
$userEmails = @("user1@domain.com", "user2@domain.com", "user3@domain.com")

# Connect to SharePoint
Connect-PnPOnline -Url $siteUrl -UseWebLogin

# Add users to the group
foreach ($email in $userEmails) {
    Add-PnPUserToGroup -Group $groupName -LoginName $email
    Write-Host "User $email added to $groupName."
}
Add Users from a CSV File

This script reads user emails from a CSV file and adds them to a group. Ensure the CSV file contains a column named Email.

Example CSV content:

Email
user1@domain.com
user2@domain.com
user3@domain.com

PowerShell Script:

# Variables
$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$groupName = "Site Members"
$csvPath = "C:\Users\YourUsername\users.csv"

# Connect to SharePoint
Connect-PnPOnline -Url $siteUrl -UseWebLogin

# Read CSV and add users
$users = Import-Csv -Path $csvPath
foreach ($user in $users) {
    Add-PnPUserToGroup -Group $groupName -LoginName $user.Email
    Write-Host "User $($user.Email) added to $groupName."
}

Use Cases
  1. Onboarding New Employees Automate the addition of new employees to relevant SharePoint groups based on their department or role.
  2. Project-Based Group Management Quickly assign team members to project-specific SharePoint groups.
  3. Periodic Audits Regularly update SharePoint groups by adding or removing users in bulk using scheduled scripts.
  4. Integration with HR Systems Fetch user details from an HR database or system to automate group management.

Error Handling

To handle errors gracefully, include try-catch blocks in your script:

try {
    Add-PnPUserToGroup -Group $groupName -LoginName $userEmail
    Write-Host "User $userEmail added successfully."
} catch {
    Write-Host "Error adding user $userEmail: $_"
}

Using PowerShell scripts for managing SharePoint group memberships is a powerful way to streamline operations and minimize errors. Whether you’re handling individual users or large batches, these scripts offer flexibility and efficiency.


Accounting.js Branding Cascading StyleSheet Cheat Sheet Competitors Connect Content Type CSS Currency Date Formats Dates Flows Hillbilly Tabs HTML5 Javascript JavsScript JSON Format View NodeJs Numeral.js O365 Office 365 OneDrive Out Of The Box Overflow Permissions PnP PowerAutomate Power Automate PowerShell Pwermissions ReactJs Rest Endpoint Send an HTTP Request to SharePoint SharePoint SharePoint Modern SharePoint Online SharePoint Tabs ShellScript SPFX SPO Styling Sync Teams App Transform JS TypeScript

Leave a Comment

Your email address will not be published. Required fields are marked *