Configure / Security

Add bulk users to SharePoint site groups from CSV.

Add bulk users to SharePoint site groups from CSV.

Summary

This sample shows how to add bulk users to SharePoint site groups from CSV.

Bulk Users CSV

You can download input CSV reference file at here.

Example Screenshot

Implementation

  1. Open Windows PowerShell ISE
  2. Create a new file and write a script
  3. Now we will see all the steps which are required to achieve the solution:
    • Create a function to read a CSV file and store it in a global variable.
    • Create a function to connect the M365 admin site.
    • Create a function to add users to a group, in this first we will be looping all elements and connecting to the particular site. After that, we will check if the current user exists in the current group. If not exists then we will add it.
powershell
$AdminSiteURL = "https://domain-admin.sharepoint.com/"
$Username = "chandani@domain.onmicrosoft.com"
$Password = "********"
$SecureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force 
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
$CSVPath = "E:\Contribution\PnP-Scripts\bulk-add-users-to-group\SP-Usres.csv"
$global:CSVData = @()

Function Login() {
    [cmdletbinding()]
    param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Creds)     
    Write-Host "Connecting to Tenant Admin Site '$($AdminSiteURL)'" -ForegroundColor Yellow   
    Connect-PnPOnline -Url $AdminSiteURL -Credentials $Creds
    Write-Host "Connection Successful!" -ForegroundColor Green 
    ReadCSVFile
}

Function ReadCSVFile() {
    Write-Host "Reading CSV file..." -ForegroundColor Yellow   
    $global:CSVData = Import-Csv $CSVPath
    Write-Host "Reading CSV file successfully!" -ForegroundColor Green   
    AddUsersToGroups
}

Function AddUsersToGroups() {
    ForEach ($CurrentItem in $CSVData) {
        Try {
            #Connect to SharePoint Online Site
            Write-host "Connecting to Site: "$CurrentItem.SiteURL
            Connect-PnPOnline -Url $CurrentItem.SiteURL -Credentials $Creds
  
            #Get the group
            $Group = Get-PnPGroup -Identity $CurrentItem.GroupName
  
            #Get group members
            $GroupMembers = Get-PnPGroupMembers -Identity $Group | select Email
            
            #Check if user is exists in a group or not
            $IsUserExists = $GroupMembers -match $CurrentItem.Users
            if ($IsUserExists.Length) {
                Write-Host "User $($CurrentItem.Users) is already exists in $($Group.Title)" -ForegroundColor Yellow                
            }
            else {
                Write-Host "Adding User $($CurrentItem.Users) to $($Group.Title)" -ForegroundColor Yellow  
                Add-PnPGroupMember -LoginName $CurrentItem.Users -Identity $Group
                Write-host "Added User $($CurrentItem.Users) to $($Group.Title)" -ForegroundColor Green
            }                        
        }
        Catch {
            write-host "Error Adding User to Group:" $_.Exception.Message -ForegroundColor Red 
        }
    }
}

Function StartProcessing {
    Login($Creds);    
}

StartProcessing

Contributors

Author(s)
Chandani Prajapati
Ganesh Sanap

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Tool documentation & references

Keep exploring