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

  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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex GreenProject Systems EngineerCommented:
What is it you're wanting to do? Export sites owners from what?

Alex GreenProject Systems EngineerCommented:
If it's sharepoint

$webapp = Get-SPWebApplication
$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

sharepoint0520Author Commented:
 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,
sharepoint0520Author Commented:
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
    Write-Host "Completed"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.