Add Owner to SharePoint Online List PowerShell

This script uses SharePoint Client Side Object Model to Add Owner to selected SharePoint Online Libraries and Lists.  You need to install the latest SharePoint Online Client Components from Github or download them from Microsoft download center.  You can change the script to only find list by title and then add the owner.  The script creates the required objects and then add the User as owner. 

$User = “user@domain.onmicrosoft.com”
#$User = Read-host “Please enter Office365 Admin User name username@domain.onmicrosoft.com. “
$Creds = Get-Credential

Connect-SPOService -Url https://tenant-admin.sharepoint.com -Credential $Creds
$site = Get-SPOSite https://site.sharepoint.com/teams/HCPTS

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’

#Get the Client Context and Bind the Site Collection
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)

#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName , $Creds.Password)
$ctx.Credentials = $credentials

#Fetch the users in Site Collection

$Web = $ctx.Web;
$ctx.Load($Web)
$Lists = $Web.Lists
$ctx.Load($Lists)
$ctx.ExecuteQuery()

$SAPLists = $Lists | ?{$_.Title -Like “SAP Data Quality Review*” } | Select Title
$SAPLists | Select Title

foreach($alist in $SAPLists)
{

   Write-Host $alist.Title
  $OwnersGroupTitle = “Site Owners”
  $OwnerGroup = $Web.SiteGroups.GetByName($OwnersGroupTitle)

  $FullControl = $web.RoleDefinitions.GetByName(“Full Control”)
  # Create a role assignment and apply the ‘read’ role.
  $roleAssignment = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
  $roleAssignment.Add($FullControl)

 
  $SelectedList = $Web.Lists.GetByTitle($alist.Title)
  $ctx.Load($SelectedList)
  $ctx.Load($SelectedList.RoleAssignments.Add($OwnerGroup,$roleAssignment))
  $SelectedList.Update()
  $ctx.ExecuteQuery()
}

Script is also available below

https://gallery.technet.microsoft.com/Add-Owner-to-SharePoint-4f13d94f

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s