Add-PnPSiteCollectionAdmin: The remote server returned an error: (401) Unauthorized.

Yesh Rajawat 115 Reputation points
2023-08-11T09:30:32.78+00:00

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"
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,481 Reputation points Microsoft External Staff
    2023-08-14T02:26:41.82+00:00

    Hi @Yesh Rajawat ,

    To connect pnponline, you should use following cmdlet

    Connect-PnPOnline [yourtenant].sharepoint.com -ClientId [clientid] -Tenant [yourtenant].onmicrosoft.com -CertificateBase64Encoded [pfx base64 encoded]
    

    Here is the documents for more details, please make a reference

    https://pnp.github.io/powershell/articles/authentication.html

    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.