Script to export Primary and Secondary Site owner to excel by powershell script

sharepoint0520
sharepoint0520 used Ask the Experts™
on
Experts
  We have to write the powershell script to export Primary and Secondary site owners to excel. Can you help me to write the script to export to excel?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
What is it you're wanting to do? Export sites owners from what?

Thanks
Alex
AlexSenior Infrastructure Analyst

Commented:
If it's sharepoint

$webapp = Get-SPWebApplication https://SharePoint.domain.com
$sites = $webapp | Get-SPSite -Limit All
$webs =  $sites | Get-SPWeb -Limit All
foreach ($web in $webs) {
    $users = $web.siteusers
    write-host "Processing " $web.url -foregroundcolor cyan
    write-host "Site Admins:" -foregroundcolor yellow
    foreach ($user in $users) {
        if ($user.IsSiteAdmin) {
            write-host $user.Name
        }
        #write-host $user.Name "IstSiteAdmin: " $user.isSiteAdmin
    }
    Write-host "Site Owners" -foregroundcolor green
    foreach ($user in $users) {
        if ($web.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask)) {
            write-host $user.Name
        }
    }
}

Open in new window

Author

Commented:
Alex,
 I have couples of questions.
   1.It will grab Primary and Secondary Site Owners?
   2. It will export to excel or txt file?
   3. It will work for two web application?

Please advice,
if ((Get-PSSnapin  "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$outputFile = "C:\\Report.txt"
$webapp = "http://web1/"
$Web = Get-SPWebApplication $webapp
$text = ""
$text | out-file $outputFile
foreach ($sites in $Web.sites) {
     <#$text = "-------------------------------------------"
     $text | out-file $outputFile -append
        $text = "Site: " + $sites.url
        $text | out-file $outputFile -append
     $text = "Primary Owner: " + $sites.owner.Name
        $text | out-file "$outputfile" -append
     $text = "Secondary Owner: " + $sites.secondarycontact.Name
        $text | out-file "$outputfile" -append
     $text = "-------------------------------------------"
        $text | out-file "$outputfile" -append#>

        $addlOwners = $sites.RootWeb.SiteAdministrators
        $owners = ""
        foreach($owner in $addlOwners)
        {
            $owners += $owner.Name + ";"
        }

        "$($sites.url),$($sites.owner.Name),$($sites.secondarycontact.Name),$($owners)" | Out-File C:\SiteOwnerReport.csv -Append
        $sites.url
       
    }
    Write-Host "Completed"

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial