2

I have an Office 365 group added, for e.g. "ABC" which could be present at any of the below level. Permissions are broken at any level below

Site
  Document Library
  Pages
    Page 1
    Page 2
  SubSite

I need to replace this Office 365 group with another one for e.g. "DEF". Using Powershell, i can remove the user from the site collection. But because i do that, the user is removed from all places, then i cant find out where the user was present and replace them.

          $usrold =$web.SiteUsers.GetByLoginName($O365GroupOld)
          $ctx.Load($usrold)
          $ctx.ExecuteQuery()
          $web.SiteUsers.Remove($usrold)
          $ctx.ExecuteQuery()
          $usernew=$web.EnsureUser($O365GroupNew)
          $ctx.Load($usernew)
          $ctx.ExecuteQuery()
          $roleDefinition = $ctx.Site.RootWeb.RoleDefinitions.GetByType([Microsoft.SharePoint.Client.RoleType]::Contributor)  
          $roleBindings = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx) 
          $roleBindings.Add($roleDefinition)
          $ctx.Load($web.RoleAssignments.Add($usernew, $roleBindings))
          $ctx.ExecuteQuery()

Can someone provide me a powershell script that can replace the Office 365 groups that exists at any level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.