PowerShell Script To Display All SharePoint Site Collection Administrators In Web Application

In this post I present a script that will display all of the site collection administrators for a given web application.  This script will work for SharePoint 2007 or 2010 as it uses the object model rather than the new SharePoint 2010 commandlets.  Special thanks to Tasha Scott (Twitter) for posting a request for this script.  It took less than 15 minutes to come up with and formalize.

tweet1

Solution

The solution is fairly straight forward.  First you grab a reference to a site collection.  Get the web application from that.  Then loop through all of the site collections within the web application.  For each site collection iterate over the SiteAdministrators property for the RootWeb.  Then write out the site url and admin display names.  The script below is the condensed version, but the version on the Script Repository is a bit fleshed out.

Click here for link to TechNet Script Repository full version

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

Conclusion

As in past times a friend on Twitter has run into a roadblock, requested help, and I was able to come up with a PowerShell script in a short amount of time to solve the problem.  I really enjoy the SharePoint community and how it can band together when situations like this arise.  Hopefully you’ll use this script and share some of your own in the future.  For now enjoy documenting the site collection admins in your farm.

-Frog Out

10 thoughts on “PowerShell Script To Display All SharePoint Site Collection Administrators In Web Application

  1. Originally posted on: https://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx#571168Also, I can at this point attest to the successfulness of this script- we were able to get exactly what we needed with only one minor change- From this: Write-Host “$($siteAdmin.ParentWeb.Url) – $($siteAdmin.DisplayName)”To this: Write-Host “$($siteAdmin.ParentWeb.Url) – $($siteAdmin.Name)”With DisplayName we were getting blank/null values; prolly something in our setup. Thanks again! At some point I hope I’m able to give you a hug!

    Like

  2. Originally posted on: https://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx#586655Any way to grab all WebAps and Site collections from a farm without providing anything so that we can report all stale sites on a MOSS 2007 farm? The below link is does this but you have to supply each site collection url. Thanks.http://ecsdukece.wordpress.com/2011/06/30/using-powershell-to-find-stale-sharepoint-sites/#comment-13

    Like

  3. Originally posted on: https://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx#587376jcnet, If you follow this link to another post I wrote it goes through the process of enumerating all web apps, site collections, and sites within a farm. All you need to do is supply one url for the farm you want to hit. Works in 2007 and 2010. Let me know if that works or not.https://briantjackett.com/archive/2009/04/24/the-power-of-powershell-and-sharepoint-enumerating-sharepoint-permissions-and.aspx

    Like

  4. Originally posted on: https://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx#610134Jeff, If you combine the above script with the full farm traversal pattern in the below blog post you should be able to get what you are looking for. Let me know if issues with that.https://briantjackett.com/archive/2011/09/19/powershell-script-to-traverse-all-sites-in-sharepoint-2010-or.aspx

    Like

  5. Originally posted on: https://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx#626228This doesn’t get all the subsites, you have to loop through the AllWebs to get them all:$thesite = get-spsite “http://x.x.x.x” #thesite is an SPSite$theWebApp = $thesite.webapplication #SPWebApplicationforeach ($site in $theWebApp.sites) #SPSite{ foreach ($web in $site.AllWebs) #SPWeb { foreach ($siteAdmin in $web.SiteAdministrators) #SPWeb { Write-Host “$($siteAdmin.ParentWeb.Url) – $($siteAdmin.DisplayName)” } } $site.Dispose() }$thesite.Dispose()

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s