Get Site Collection Administrators

For one activity I had to find all Site Collection Administrators for each and every web application. since I had only few web applications. I managed get it done with the help of bellow link.

https://sharepoint.stackexchange.com/questions/209841/get-site-collection-administrators-using-powershell

after few modifications, now this script will generate a .csv file with site name and all site collection administrators for each site collection.

Add-PSSnapin Microsoft.SharePoint.PowerShell
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

$webApp = Get-SPWebApplication -Identity "http://webappURL"

$logFilePath = "FilePath\$($webApp.Name)-Log.log";

Add-Content -Path $logFilePath -value "SiteName | Admin Name";

$siteCollections = $webApp.Sites ;

foreach($spSite in $siteCollections)
{

  $siteURL =  $spSite.Url;

  $siteCollectionAdmins = $spSite.RootWeb.SiteAdministrators;

    foreach ($admin in $siteCollectionAdmins)
    {

      Write-Host $spSite.Url "|" $admin.DisplayName;
      Add-Content -Path $logFilePath -value " $($siteURL) | $($admin.DisplayName)";

    }

  $spSite.Dispose();

}

Hope This will help someone.

Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *