GitHub Icon Image
GitHub

Create SharePoint Groups in Bulk using CSV file

Summary

This script shows how to create multiple groups in SharePoint for different site collection providing input from CSV file.

Implementation

  • Open Windows PowerShell ISE
  • Create a new file
  • Write a script as below,
  • Create a CSV using the provided sample. Two columns are required SharePointUrl and SharePointGroupName
  • Then we provide the path of the file as input to the PowerShell when prompted in the window when executed.

Download Sample CSV from this link

Execution Image and output Image

ExecutionImage

OutPutImage

  • PnP PowerShell
  • CLI for Microsoft 365
  • SPO Management Shell

#Uncomment below line if facing remote server 400 error
#Import-Module SharePointPnPPowerShellOnline -UseWindowsPowerShell

$filePath = Read-Host "Please provide CSV file path"

# Import CSV file from given path
$importedFile = Import-Csv -Path $filePath

for ($index = 0; $index -lt $importedFile.Count; $index++) {
    # Connect to SharePoint online site
    Connect-PnPOnline -Url $importedFile[$index].SharePointUrl -Interactive

    # Create a new group in SharePoint site
    New-PnPGroup -Title $importedFile[$index].SharePointGroupName
}

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
   m365 login
}

$filePath = Read-Host "Please provide CSV file path"

# Import CSV file from given path
$importedFile = Import-Csv -Path $filePath

for ($index = 0; $index -lt $importedFile.Count; $index++) {
	# Create a new group in SharePoint site
	m365 spo group add --webUrl $importedFile[$index].SharePointUrl --name $importedFile[$index].SharePointGroupName
}

# Disconnect SharePoint online connection
m365 logout

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365


# SharePoint online admin center site url
$adminSiteUrl = "https://contoso-admin.sharepoint.com"

# Connect to SharePoint online admin center
Connect-SPOService -Url $adminSiteUrl

$filePath = Read-Host "Please provide CSV file path"

# Import CSV file from given path
$importedFile = Import-Csv -Path $filePath

for ($index = 0; $index -lt $importedFile.Count; $index++) {
	# Create a new group in SharePoint site
	New-SPOSiteGroup -Site $importedFile[$index].SharePointUrl -Group $importedFile[$index].SharePointGroupName -PermissionLevels "Read"
}

# Disconnect SharePoint online connection
Disconnect-SPOService

Check out the SPO Management Shell to learn more at: Introduction SharePoint Online Management Shell | Microsoft Docs

Contributors

Author(s)
Kunj Sangani
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.

Back to top Script Samples
Generated by DocFX with Material UI