Adding SharePoint Site Owner in PowerShell

Copper Contributor

Hi,
I have made powershell script to provision modern team and communication sites.
I have a share point list where user selects relevant information for this site.
The user also selects the owner and administrators for this site and they should be updated immediately after the new site is provisioned.

I have tried the below code but it doesn't work.

[Microsoft.SharePoint.Client.FieldUserValue] $owner = $item["Owner"]
$owneremail = $owner.Email
Connect-PnPOnline $siteURL
#Now we have access on the SharePoint site for any operations
       $context_2 = Get-PnPContext
       $web = Get-PnPWeb
       $web
       $context_2.Load($web, $web.WebTemplate)
       $context_2.ExecuteQuery()
       Add-PnPSiteCollectionAdmin -Owners $owneremail

 

 

Also tried

$siteURL = "https://SomeSite.sharepoint.com/sites/SubSite1"

Connect-PnPOnline $siteURL

Set-PnPSite -Identity $siteURL -Owners "User@SomeSite.com"

 

 


Kindly assist with the same.

1 Reply

Hi, have you tried using the "Set-SPOUser" from SPO Management Shell instead of PnP ?

 

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/set-spouser?view=sharepoint-ps

 

Try:

Connect-SPOService -Url "https://somesite-admin.sharepoint.com/"

Set-SPOUser -LoginName "User@SomeSite.com" -IsSiteCollectionAdmin $true -Site $siteURL