SharePoint Online: Grant Site Owner Permission to a User with PowerShell

Requirement: Grant Site Owner Permissions to a User in SharePoint Online

How to Grant Site Owner Permissions to a User in SharePoint Online?
By adding people to SharePoint Online site owners group, they can manage the site and perform typical site administration activities such as setup site branding and structure, add or remove users, Managing content, etc. To add a site owner to your SharePoint Online site, follow these steps:
  • Navigate to your SharePoint Online site, Click on Settings Gear >> and then click on "Site Permissions" link
  • On the Site Permissions pane, Click on "Share Site" button.
    sharepoint online site owner powershell
  • Enter the user name in the search box and pick the user >> Select "Full Control" permission for the user and click on "Add" button to grant the user site owner permissions.
    add site owner sharepoint online powershell
Alternatively, You can go to Site Settings >> Site Permissions >> Pick the relevant site owners group >> Click on "New" >> Add User to Group.

Add Site Owner in SharePoint Online using PowerShell
Here is how to add a user to Owner group using PowerShell in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/warehouse"
$UserAccount = "[email protected]"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
      
    #Get the Default Owner Group of the site
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AssociatedOwnerGroup)
    $Ctx.ExecuteQuery()
 
    #ensure the sharepoint online user
    $User=$web.EnsureUser($UserAccount)
  
    #sharepoint online powershell add user to owner group
    $Web.AssociatedOwnerGroup.Users.AddUser($User) | Out-null
    $Ctx.ExecuteQuery()
  
    write-host  -f Green "User '$UserAccount' has been added to Owners Group of the Site!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

SharePoint Online: PnP PowerShell to Add Site Owner
We can add site owner with PnP PowerShell as well, as:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Warehouse"
$UserAccount= "[email protected]"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
 
#Get the Default Owners Group of the site
$OwnersGroup = Get-PnPGroup  -AssociatedOwnerGroup
 
#SharePoint Online pnp powershell to add site owner
Add-PnPUserToGroup -LoginName $UserAccount -Identity $OwnersGroup

Here is my another post on adding user permissions on Office 365 group site: SharePoint Online: How to Add User to Office 365 Group Site?

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.