How can I get the Site Collection Administrators using PowerShell in SharePoint 2013? I have tried the below script but it just returns me to the prompt with no results:
$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()
Enter Site URL: http://myurl.local
EDIT: The reason I was not getting any results was because I didn't have SharePoint_Shell_Access on the SP database. I would have expected PowerShell to throw an error when adding the SP snap-in but it didn't.
$site. You are disposing objects, that were created by different process and not by you withnewkeyword.