If you need to add a user as a site collection administrator across all these sites, doing it manually can be time-consuming and prone to errors. Thankfully, PowerShell and the PnP PowerShell module can help automate this task efficiently. In this blog post, I’ll guide you through a script that adds a user as a site collection administrator to all sites connected to a SharePoint Online hub.
Script Overview
This PowerShell script does the following:
- Connects to the SharePoint Online Admin Center.
- Retrieves all sites connected to the specified hub site.
- Adds the specified user as a site collection administrator to each connected site.
- Disconnects from the SharePoint Online Admin Center upon completion.
Prerequisites
Before running the script, make sure you have the following:
- PnP PowerShell Module: Install the latest version of the PnP PowerShell module (PnP.PowerShell).
- Admin Permissions: Ensure you have SharePoint Admin permissions to execute the script.
The Script
Here is the script:
########################################################################################
# Description: This script adds a user as a site collection administrator to all
# sites connected to a hub site in SharePoint Online.
########################################################################################
param(
[Parameter(Mandatory = $true)]
[string]$adminEmail = "", # Email of the user to be added as site collection administrator
[Parameter(Mandatory = $true)]
[string]$hubSiteUrl = "", # URL of the hub site
[Parameter(Mandatory = $true)]
[string]$adminSiteUrl = "" # URL of the SharePoint Online Admin Center
)
# Connect to the SharePoint Online Admin Center
Connect-PnPOnline -Url $adminSiteUrl -Interactive
# Get all sites connected to the hub site
$connectedSites = Get-PnPHubSiteChild -Identity $hubSiteUrl
# Loop through each site and add the specified email as a site collection administrator
foreach ($site in $connectedSites) {
Write-Host "Processing site: $($site)"
try {
# Add the site collection administrator
Set-PnPTenantSite -Owners $adminEmail -Identity $site
Write-Host "Added $adminEmail as site collection administrator for $($site)"
}
catch {
Write-Error "Failed to process site $($site): $_"
}
finally {
}
}
# Disconnect from the SharePoint Online Admin Center
Disconnect-PnPOnline
Write-Host "Script completed."
Key Functions in the Script
- Connecting to SharePoint Online: The
Connect-PnPOnlinecommand establishes a connection to the SharePoint Online Admin Center. The-Interactiveparameter prompts for credentials. - Retrieving Sites Connected to the Hub: The
Get-PnPHubSiteChildcommand retrieves all sites associated with the specified hub site. - Adding a Site Collection Administrator: The
Set-PnPTenantSitecommand adds the specified user as a site collection administrator to each site. - Error Handling: The script includes error handling (
try-catchblocks) to ensure that any issues encountered while processing a site are logged, and the script continues to run for the remaining sites.
How to Run the Script
- Open PowerShell in Administrator mode.
- Run the script with the necessary parameters
.\Add-SiteCollectionAdmin.ps1 -adminEmail "user@domain.com" -hubSiteUrl "https://yourtenant.sharepoint.com/sites/YourHubSite" -adminSiteUrl "https://yourtenant-admin.sharepoint.com"
Conclusion
By using this PowerShell script, you can easily automate the addition of a site collection administrator to all sites connected to a SharePoint Online hub. This helps maintain consistent administrative access, saves time, and reduces the likelihood of manual errors. Feel free to adapt and extend the script according to your specific organizational needs.
Additional Resources:
Disclaimer
This script is provided as-is, without any guarantees or warranties. Use it at your own risk. Please be aware that SharePoint Online environments can vary significantly based on custom configurations, policies, and organizational requirements. It is highly recommended to thoroughly review and test the script in a controlled environment before deploying it in production. Make any necessary modifications to suit your specific setup and always ensure you have appropriate backups and permissions before running any scripts that modify your SharePoint environment. The author is not responsible for any issues or data loss that may occur from the use of this script.