Forum Discussion
Adding SharePoint Site Owner in PowerShell
Hi all,
I am working on a provisioning script that will allow me to create Modern SharePoint Team Sites and apply a provisioning template to them after, all of this in PowerShell Cmdlets.
I create my Modern Team Site by creating a new Unified Group (New-PnPUnifiedGroup cmdlet).
I need to add an member to the Owner Group of the SharePoint site but I can't seem to make it work. The only way I have been able to achieve this is by applying a provisioning template that adds the user to the admin group. The problem with this is that it takes a random amount of time between 5 minutes and 5 hours before I can apply the template to my newly created site (before that, I always get ''Access Denied'' errors).
I have tried $tenant.SetSiteAdmin(), Set-SPOUser -IsSiteCollectionAdmin and a few other things, but nothing has worked yet.
Is there a way to know how this is done in the code of the Apply-PnPProvisioningTemplate cmdlet?
You can use the "Add-PnPUserToGroup" PnP Powershell cmdlet to handle this.
https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPUserToGroup.md
Link to download powershell cmdlets:
https://github.com/SharePoint/PnP-PowerShell/releases
8 Replies
You can use the "Add-PnPUserToGroup" PnP Powershell cmdlet to handle this.
https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPUserToGroup.md
Link to download powershell cmdlets:
https://github.com/SharePoint/PnP-PowerShell/releases
- Marc DepelteauCopper Contributor
Hi Drew Madelung,
I did try this before and it added my user to the Owner group, but it still says ''access denied'' when I try to apply the provisioning template.
Perhaps it could be that the access token has not been refreshed/renewed?- So you are trying to add an account to the owners group which works with the powershell above then you are trying to use that same account to apply a provisioning template to a site?
- Marc DepelteauCopper Contributor
Bump