Quick Tip: Use PowerShell to Retrieve SharePoint Site Owners


One afternoon my boss send me an email asking for a list of all the site owners on our SharePoint farm. If we only have one farm, one site collection, and a few sites, I could easily log on and find these owners from each of the sites. However given that we have over 100 sites, this was not an option for me. Furthermore, I may have to repeat this same process if he asks for it a few days, weeks or months later.

So a better and smarter way was to use PowerShell to query the SharePoint farm to retrieve all the site owners. Here’s the PowerShell script to retrieve all site owners for a SharePoint farm and export them to a CSV file:


Get-SPSite -Limit All |
   Get-SPWeb -Limit All |
   where { $_.HasUniquePerm -and $_.AssociatedOwnerGroup -ne $null } |
   foreach { $SiteURL = $_.Url; $SiteTitle= $_.Title; $_ } |
   Select -ExpandProperty AssociatedOwnerGroup |
   Select -ExpandProperty Users |
   Select @{Label=”Site URL”; Expression={$SiteURL} }, @{Label=”Site Title”;
   Expression={$SiteTitle} }, DisplayName, Email |
   Export-Csv -NoTypeInformation -path "path-and-csv-file-name.csv"

Save the above PowerShell script into a file, e.g. get-site-owners.ps1. Then do the following to execute it:

  • Copy to the server where it is to be executed
  • Launch a SharePoint 2010 Management Shell using Run as administrator
  • Change directory to the folder where the ps1 file was placed
  • Executed the file by typing the following at the SharePoint PS prompt:
    • ./get-site-owners
  • Confirmed that the information has been saved to the CSV file

Notes:

  • Get-SPSite gets all site collections
  • Get-SPWeb gets all sites
  • @{Label=”custom label name”; Expression={$variable}} to customise column display label
  • Export-CSV to export output to a csv file
  • -NoTypeInformation to exclude the type information from the first line in the exported csv file
  • Uses the pipe | to chain a serious of action with out of previous action serving as input to the next action
Advertisements

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