List Site Owners for All Site Collections

Here’s a Powershell script I’ve managed to piece together that will output all Site Collection Owners (Not Site Collection Administrators) into either a csv or xml file.  In our SharePoint deployment we’re more of a service provider, hosting multiple web applications each containing one to many site collections, and we were looking for an easy way to determine who the site owners were for every site so that we could send notification prior to maintenance windows etc.  I’ve been working on this in my spare time over the past few weeks, feel free to leave any suggestions, I know it’s a bit convoluted.

#Generates a list of all site collections across all Web Applications,
#and outputing them into an array

$sitepath = New-Object System.Collections.ArrayList
$output=stsadm -o enumzoneurls
$x=[xml]$output
$x.ZoneUrls.Collection|
foreach-object -process {$y=stsadm -o enumsites -url $_.Default;$sites=[xml]$y;$sites.Sites.Site}|
foreach-object -Process {$sitepath=$sitepath + $_.Url}
$sitepath | Write-Output

#Define Output file, and labels

$filename = AllSiteUsers.csv
Write-Output “Site Collection,Title,Group,UserID,Email Address” | Out-File $filename -Append

#Loops through all site collections generating a list of Site Owners

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
foreach ($object in $sitepath) {
$site=new-object Microsoft.SharePoint.SPSite($object)
$web=$site.Rootweb
$groups = $web.groups | ? {$_.Name -match “^.*Owners” }
foreach($group in $groups)
{
foreach($user in $group.users)
{
Write-Output “$($object),$web,$($group.Name),$user,$($user.Email)” | Out-File $filename -Append
$user
}
}}

#Converts the csv ouput to XML

Import-Csv AllSiteUsers.csv | Export-Clixml AllSiteUsers.xml

12 Responses to “List Site Owners for All Site Collections”

  1. Tried running this and got an error, how do you run this via command line

  2. Not too familiar with powershell tried this:

    E:\Software\Powershell>powershell getsiteowners.ps1
    The term ‘getsiteowners.ps1′ is not recognized as a cmdlet, function, operable
    program, or script file. Verify the term and try again.
    At line:1 char:17
    + getsiteowners.ps1 <<<<

  3. rolandserman Says:

    all you should have to do, is open powershell, browse to the folder the script is located in, just like you would from a command prompt. then run type the following:

    ./getsiteowners.ps1 (that is dot forward slash, then name of the script)

    If you get an error about execution policy, then you need to run the following:

    set-executionpolicy unrestricted

    And then run the script again.

  4. Now im getting the following:

    PS E:\Software\Powershell> .\getsiteowners.ps1
    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Unexpected end of file has occurred. The following elements are not closed: Sites. Line 3, position 84.”
    At E:\Software\Powershell\getsiteowners.ps1:8 char:79
    + foreach-object -process {$y=stsadm -o enumsites -url $_.Default;$sites=[xml]$y; <<<< $sites.Sites.Site}|

  5. rolandserman Says:

    Do me a favor, from your powershell prompt type the following:

    stsadm -help enumsites

    Do you get the help for enumsites? or do you get an error about not recognizing stsadm as a cmdlet?

  6. I get the help options:
    PS E:\Software\Powershell> stsadm -help enumsites

    stsadm.exe -o enumsites
    -url
    -showlocks
    -redirectedsites
    [-databasename ]

  7. rolandserman Says:

    Actually, after re-reading your error (The following elements are not closed), it looks like you’re missing a bracket. Give me a few minutes to pull this over to one of my SharePoint servers, and make sure I didn’t miss something when I pasted it into my blog.

  8. rolandserman Says:

    hmmm, are you running this on Server 2003, or Server 2008?

  9. rolandserman Says:

    By the way, if you’re running the script on 2008, you need to run Powershell as Administrator. Otherwise it will fail with a bunch of misleading errors.

  10. This is Server 2003

  11. Cool site, love the info.

Leave a Reply