Is there any PowerShell that can read from a CSV file and create SharePoint groups and add members like this?

frob 4,231 Reputation points
2021-08-09T22:12:10.17+00:00

Hi there

Is there any PowerShell that can read from a CSV file and create SharePoint groups and add members? CSV file looks like this:

Users, SharePointGroupName
John@Company portal .com, Group 1
John@Company portal .com, Group 2
John@Company portal .com, Group 3
Mary@Company portal .com, Group 1
Mary@Company portal .com, Group 4
Peter@Company portal .com, Group 3
Peter@Company portal .com, Group 4
Peter@Company portal .com, Group 5
and so on...

Thank you.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Marcin Policht 112.8K Reputation points MVP Volunteer Moderator
    2026-11-13T04:40:25.8633333+00:00

    Yep - you can use PowerShell with PnP.PowerShell to read the CSV, create SharePoint groups if they do not already exist, and add users to the appropriate groups. The script below assumes that the SharePoint groups are created at the site collection level and that the CSV contains users identified by their email addresses. For example, John is added to Group 1, Group 2, and Group 3, while Mary is added to Group 1 and Group 4. The script also handles existing groups and existing memberships without generating duplicate entries.

    Install-Module PnP.PowerShell -Scope CurrentUser
    

    CSV file (Groups.csv):

    Users,SharePointGroupName
    ******@company.com,Group 1
    ******@company.com,Group 2
    ******@company.com,Group 3
    ******@company.com,Group 1
    ******@company.com,Group 4
    ******@company.com,Group 3
    ******@company.com,Group 4
    ******@company.com,Group 5
    
    # SharePoint site URL
    $SiteURL = "https://company.sharepoint.com/sites/YourSite"
    
    # CSV file path
    $CSVPath = "C:\Scripts\Groups.csv"
    
    # Connect to SharePoint
    Connect-PnPOnline -Url $SiteURL -Interactive -ClientId "YOUR-ENTRA-APP-CLIENT-ID"
    
    # Import CSV
    $CSVData = Import-Csv -Path $CSVPath
    
    foreach ($Entry in $CSVData) {
    
        $UserEmail = $Entry.Users.Trim()
        $GroupName = $Entry.SharePointGroupName.Trim()
    
        # Check whether the SharePoint group exists
        $Group = Get-PnPGroup -Identity $GroupName -ErrorAction SilentlyContinue
    
        # Create the group if it does not exist
        if (-not $Group) {
    
            Write-Host "Creating SharePoint group: $GroupName" -ForegroundColor Yellow
    
            New-PnPGroup -Title $GroupName
    
        }
    
        # Retrieve current group members
        $GroupMembers = Get-PnPGroupMember -Identity $GroupName
    
        # Check whether the user is already a member
        $ExistingMember = $GroupMembers | Where-Object {
            $_.Email -eq $UserEmail -or $_.LoginName -eq $UserEmail
        }
    
        # Add user if not already a member
        if (-not $ExistingMember) {
    
            Write-Host "Adding $UserEmail to $GroupName" -ForegroundColor Green
    
            Add-PnPGroupMember -Identity $GroupName -LoginName $UserEmail
    
        }
        else {
    
            Write-Host "$UserEmail is already a member of $GroupName" -ForegroundColor Cyan
    
        }
    
    }
    

    The script creates the SharePoint groups with their default settings. It does not assign permission levels to the groups or grant them access to the site, libraries, or lists. Those permissions must be assigned separately.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    Was this answer helpful?

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.