1

How can I get the Site Collection Administrators using PowerShell in SharePoint 2013? I have tried the below script but it just returns me to the prompt with no results:

$siteUrl = Read-Host "Enter Site URL"
$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
  $spWebApp = $rootSite.WebApplication
    foreach($site in $spWebApp.Sites)
    {
      foreach($siteAdmin in $site.RootWeb.SiteAdministrators)
      {
        Write-Host "$($siteAdmin.ParentWeb.Url) - $($siteAdmin.DisplayName)"
      }
    $site.Dispose()
  }
$rootSite.Dispose()

Enter Site URL: http://myurl.local

EDIT: The reason I was not getting any results was because I didn't have SharePoint_Shell_Access on the SP database. I would have expected PowerShell to throw an error when adding the SP snap-in but it didn't.

4
  • Any specific scope? One site? Multiple sites? All sites in different web applications? Mar 6, 2017 at 15:50
  • Ideally I would target a Web App and get a list of all Site Collections and Site Collection Administrators. However I'd just be happy to get the Site Collection Administrators for a targeted Site Collection.
    – neptr
    Mar 6, 2017 at 16:17
  • added some examples as an answer. Mar 7, 2017 at 8:18
  • You shold not dispose $site. You are disposing objects, that were created by different process and not by you with new keyword. Dec 27, 2018 at 11:24

4 Answers 4

1
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue    
    $siteUrl = Read-Host "Enter Site URL"
    $rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
    $spWebApp = $rootSite.WebApplication
    foreach($site in $spWebApp.Sites)
    {
        foreach($siteAdmin in $site.RootWeb.SiteAdministrators)
        {
            Write-Host "$($siteAdmin.ParentWeb.Url) - $($siteAdmin.DisplayName)"
        }
        $site.Dispose()
    }
    $rootSite.Dispose()
4
  • I found this one too but it did not work for me. It does not list any results.
    – neptr
    Mar 6, 2017 at 16:23
  • Did you run powershell as administrator?
    – Marko Tica
    Mar 7, 2017 at 9:13
  • I always run PS as administrator :)
    – neptr
    Mar 7, 2017 at 11:09
  • 1
    It turns out I did not have SP Shell Access. I thought I did. Thanks for your support!
    – neptr
    Mar 7, 2017 at 12:49
1

The reason I was not getting any results was because I didn't have SharePoint_Shell_Access on the SP database. I would have expected PowerShell to throw an error when running "Add-PSSnapin Microsoft.SharePoint.PowerShell" but it didn't.

0

While this piece of Powershell will work and return all site collections in a webapplication:

Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

$siteUrl = Read-Host "Enter Site URL"

$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$spWebApp = $rootSite.WebApplication

foreach($site in $spWebApp.Sites)
{
    foreach($siteAdmin in $site.RootWeb.SiteAdministrators)
    {
        Write-Host "$($siteAdmin.ParentWeb.Url) - $($siteAdmin.DisplayName)"
    }
    $site.Dispose()
}
$rootSite.Dispose()

This will work as well.

For a specific SharePoint Site collection:

Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

$siteUrl = Read-Host "Enter Site URL"

Get-SPSite -Identity $siteUrl | select-object url, Owner, SecondaryContact

Or all Site collections in a webapplication:

Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

$webApp = Read-Host "Enter Webapplication URL"

Get-SPWebApplication -Identity $webApp | Get-SPSite -limit all | select-object url, Owner, SecondaryContact
5
  • The last two scripts appear to be a step closer because they list the site URL's but under the "Owner" and "SecondaryContact" headers there are no results, Does it work for you?
    – neptr
    Mar 7, 2017 at 10:18
  • @Rubio My bad, had an error from my tests. Yes i do get my primary and secondary site collection administrators. Owner is the primary SCA and SecondaryContact is the second SCA. Which script did you try? Mar 7, 2017 at 10:25
  • I tried the second and third scripts
    – neptr
    Mar 7, 2017 at 11:08
  • Still no results? Mar 7, 2017 at 11:30
  • Do you enter a sitecollection URL? Like something/sites/sitecollection ? Mar 7, 2017 at 11:30
0

Below worked for me

   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();

}}

full script I have published get-site-collection-administrators this script will generate a CSV file on specified output path

1
  • Thank you Ganesh Sanap for format correctingthe Code Section
    – theblogger
    Mar 22, 2020 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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