I'm encountering an issue while using the Add-PnPSiteCollectionAdmin
cmdlet in SharePoint PnP PowerShell. The error message I'm receiving is:
"Add-PnPSiteCollectionAdmin: The remote server returned an error: (401) Unauthorized."
Here's my scenario:
- I'm connecting to SharePoint PnP PowerShell using client credentials (Client ID and Client Secret).
- I've registered an enterprise application in Azure AD.
- The application has been granted the "Site.FullControl" permission for SharePoint.
- I've obtained the Client ID and Client Secret from the application's configuration.
- The initial connection (
Connect-PnPOnline
) works with a warning
WARNING:
Connecting with Client Secret uses legacy authentication and provides limited functionality. We can for instance not
execute requests towards the Microsoft Graph, which limits cmdlets related to Microsoft Teams, Microsoft Planner,
Microsoft Flow and Microsoft 365 Groups. You can hide this warning by using Connect-PnPOnline [your parameters] -
WarningAction Ignore
- However, when I run the
Add-PnPSiteCollectionAdmin
cmdlet or other similar commands, I face the 401 Unauthorized error.
-
# Connect to SharePoint using Azure AD enterprise application credentials $clientId = "your_client_id" $clientSecret = "your_client_secret" Connect-PnPOnline -ClientId $clientId -ClientSecret $clientSecret -Url "https://contoso.sharepoint.com/sites/yoursite" # Attempt to add site collection admin Add-PnPSiteCollectionAdmin -SiteUrl "https://contoso.sharepoint.com/sites/yoursite" -Owners "user@example.com"