Wednesday, 22 October 2014

Powershell: Change SharePoint Group Owner for all site collections and subsites in a web app


Recently I came across a requirement to change the SharePoint Group owner, I googled for a while but could not find one which worked, at the end I wrote my own and I am sharing the same to help someone who might be in need of it.( tested and working :) )

Script Inputs:

$groupNameWildcard = "Owner" ( Name of the group: In this case all "Site name Owners" groups on all sites + subsites) This will specify which group owner you want to change, it is a wildcard which will check if the group name contains the said text.

$newOwner = "Account" (New Owner account e.g. "domain\user")

$webApplicationURL = "Web App URL"
(URL of the Web App where you want to change the owner for group on all site collections and subsites

The Script:

----------------------------------------------------------------------------------------------------------
$groupNameWildcard = "Owner"
$newOwner = "Account Name"
$webApplicationURL = "Web App URL"
$webApp = Get-SPWebApplication $webApplicationURL

    if($webApp -ne $null)
    {
      Write-Host "Web Application : " + $webApp.Name

       foreach($siteColl in $webApp.Sites)
        {
         if($siteColl -ne $null)
          {
             Write-Host "Site Collection : " + $siteColl.Url

    $siteURL = $siteColl.Url
        #Get Site Collection
        $site = Get-SPSite $siteURL     
      
        #Step through each site in the site collection
       $site | Get-SPWeb -limit all | ForEach-Object {
                      
                if ($_.HasUniqueRoleAssignments) {

                    $spGroups = $_.get_Groups().web.Groups
            $user = $_.EnsureUser($newOwner)
            foreach($spGroup in $spGroups)
           
            {
                        
            if ($spGroup.Name.Contains($groupNameWildcard))
            {write-host "--------------------"
                Write-host "Group Name:"$spGroup.Name
                Write-host "Current Owner:"$spGroup.Owner
                write-host "Updating Site" $_.Url
                $spGroup.Owner = $user
                $spGroup.Update();
                Write-host "New Owner:"$spGroup.Owner
                write-host "--------------------"                   
            }
           
           
            }
            $_.Update();
           
                }
       
                else {
                    write-host "Site" $_.Url "will not be modified as it inherits permissions from a parent site."
                }
           
        }
        #Display completion message and dispose of site object
        write-host "Operation Complete."
        $site.Dispose()
}
}
}

-----------------------------------------------------------------------------------

Happy Scripting :)


Saravjeet Lamba
Tags:SharePoint Scripting | SharePoint 2013 | SharePoint 2010 | Powershell | SharePoint Administration | SharePoint Security

1 comment: