Friday 23 May 2014

Powershell script to get all the site owners..




  • Copy “.bat” and “.ps1” files to some location on any WFE servers .
  • Double click the batch file which generates a csv file (c:\SiteOwnersList.csv) with the list of owners and Sites.
  • Save this file as xlsx, so that we can view it properly.


Note: You can change the csv file generation path by editing the “.bat”
 


The below script will get all the Site Owners if the site is having a group with name "SiteName+Owner"

Code for .ps1

Add-PsSnapin Microsoft.SharePoint.PowerShell

$spWeb = Get-SPweb "https://test.com"
$CommunityOwnersList = ""
write-Host "SiteName,Owners Of Site"
write-Host ","
foreach($subSite in $spWeb.Webs)
{
$Sitename = $subSite.Title

foreach($group in $subSite.Groups)
    {
        if(($group.Name -like "*Owners*") -and ($group.Name -like "*$Sitename*"))
        {
            $CommunityOwners = ""
            foreach($user in $group.Users)
            {
                $username = $user.Name

                $CommunityOwners = "$CommunityOwners  $username ; "
            }
           
        }

    }
    Write-host "$Sitename , $CommunityOwners"
$CommunityOwners = ""

    $subSite.Dispose()
}

$spWeb.Dispose()

Remove-PsSnapin Microsoft.SharePoint.PowerShell



Code for .bat (so that the above code can be executed with a single click)

 cd /d %~dp0

powershell -noexit -file    ".\test.ps1" "%CD%" >> "c:\test.csv"

pause


No comments:

Post a Comment