0

I added a user to Owners list and Site Collection Admin list in all existed Team Sites (they are over 5000) by PowerShell script. But now need to remove user from Owners list in an all sites. How can I do that?

0

SharePoint 2013:

 $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
 $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}

 foreach ($websvc in $websvcs) 
{
    foreach ($webapp in $websvc.WebApplications) 
    {
         foreach ($site in $webapp.Sites) 
         {
             foreach ($web in $site.AllWebs) 
             {

                $User  = $web.EnsureUser($UserAccount)

                $Group=$web.AssociatedOwnerGroup
                $GroupUser = $Group.Users | where {$_.UserLogin -eq $User.UserLogin}
                $Group.RemoveUser($User)
            }
         }

    }
}

SharePoint Online:

  $sites=("https://MyCompany.Sharepoint.com/sites/site1 ","https://MyCompany.Sharepoint.com/sites/site2","https://MyCompany.Sharepoint.com/sites/site3")

foreach($site in $sites)
{
    groups=Get-SPOSiteGroup -Site $site | where {$_.Title -match "GroupName"}
    $users=get-spouser -Site $site

    foreach($user in $users) 
    {
        if($user.LoginName -match "TeamMember")
        { 
            foreach ($group in $groups)
            {
                Remove-SPOUser -Site $site -LoginName $user.loginName -Group $group.LoginName
            }
        }
    }
}
2
  • Do I need to add this script to an existing powershell script and then run?
    – J.U
    Jun 12 '18 at 12:56
  • Since you have already added the users, u need to execute this separately.
    – Deepmala
    Jun 12 '18 at 13:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.