A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
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