Is there a way to use Add-PnPUserToGroup command to add a AD Security Group into a SharePoint group on SharePoint Online?

I'm writing a script which does some post-some creation tasks and just after a way to do this without writing lines and lines of code. I saw one-liner examples of adding security groups as site collection admins, but haven't found a way to add this to a SP Group.

When I try Add-PnPUserToGroup -LoginName parameter I get an:

Add-PnPUserToGroup : The object is used in the context different from the one associated with the object.

Error so I think I may be doing something daft there!

I would really appreciate help on this.

Thank you

2 Answers 2


I think you are missing the Connection parameter here you need to pass in the context of site for which you are executing the command which can be obtained by using -ReturnConnection switch

$connection = Connect-PnPOnline -Url https://contoso.sharepoint.com -Credentials (Get-Credential) -ReturnConnection

Add-PnPUserToGroup -LoginName [email protected] -Connection $connection #variable form above line
  • Thank you, will try this now
    – ova
    Sep 4, 2020 at 12:57

That cmdlet is used to add to a SharePoint group. To add to an Azure AD group, use Add-AzureADGroupMember. For Active Directory, use Add-ADGroupMember.

  • Sorry- got confused. I have to add and Azure Ad security group to a SharePoint Group
    – ova
    Sep 4, 2020 at 12:45
  • In that case, try stackoverflow.com/a/20639265/1760623.
    – user6024
    Sep 4, 2020 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.