SharePoint Online: Add Site Collection Admin to Multiple Sites from a CSV File using PowerShell
Requirement: Bulk add site collection administrators to multiple SharePoint Online sites from a CSV file.
If you’re like most SharePoint administrators, you probably want to add a site collection administrator to many of your SharePoint Online sites, to quickly grant permissions to someone! Well, PowerShell can help you to automate this task. In this blog post, we’ll show you how to use the combination of CSV file and PowerShell to add a site collection administrator to multiple SharePoint Online sites.
How to Add Site Collection Admins to Multiple SharePoint Online Sites using PowerShell?
Here is how to add site collection administrator to multiple sites using PowerShell:
Import-Module Microsoft.Online.SharePoint.PowerShell
#Parameters
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$CSVFilePath = "C:\Temp\SiteCollectionAdmin.csv"
Try {
#Connect to Admin Center
Connect-SPOService -Url $TenantAdminURL
#Get data from the CSV file
$CSVData = Import-Csv $CSVFilePath
#Iterate through each row in the CSV
ForEach($Row in $CSVData)
{
Try{
#Add Site collection Admin
Set-SPOUser -site $Row.SiteURL -LoginName $Row.SiteCollectionAdmin -IsSiteCollectionAdmin $True | Out-Null
Write-host "Added Site collection Administrator to $($Row.SiteURL)" -f Green
}
Catch {
write-host -f Yellow "`tError Adding Site Collection Admin to $($Row.SiteURL) :" $_.Exception.Message
}
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}
Here is my CSV File:
You can download the CSV file from here:
PnP PowerShell to Bulk Add Site Collection Admins from a CSV
Similarly, the above script can be re-written in PnP PowerShell to add site collection administrators to multiple sites as:
#Parameters
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$CSVFilePath = "C:\Temp\SiteCollectionAdmin.csv"
Try {
#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Interactive
#Get data from the CSV file
$CSVData = Import-Csv $CSVFilePath
#Iterate through each row in the CSV
ForEach($Row in $CSVData)
{
Try{
#Add Site collection Admin
Set-PnPTenantSite -Url $Row.SiteURL -Owners $Row.SiteCollectionAdmin
Write-host "Added Site collection Administrator to $($Row.SiteURL)" -f Green
}
Catch {
write-host -f Yellow "`tError Adding Site Collection Admin to $($Row.SiteURL) :" $_.Exception.Message
}
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}
This will save you time and ensure that your site collection administrators are added in a consistent manner. If you want to add site collection administrator to all of SharePoint Online sites in your tenant, use: How to Add Site Collection Administrator to All SharePoint Online Sites in the Tenant?