Forum Discussion
michael1900
Feb 24, 2023Copper Contributor
SharePoint Online: Get the Site Owner using PowerShell
Need powershell code to Get the Site Owner using PowerShell . I tried to using below code but the data is no accurate and no incorrect . #Variables for Admin Center $AdminCenterURL = https://8...
AndySvints
Mar 04, 2023Iron Contributor
Hello michael1900,
One of the options to get Site Admins is to get list of all members of the site and then identify who has IsSiteAdmin property equal to True:
$SiteAdmins = (Get-SPOUser -Site $URL -Limit ALL | Where-Object { $_.IsSiteAdmin -eq $True}
Here is your code with the added line:
$AdminCenterURL = "https://<TenantName>-admin.sharepoint.com/"
$CSVPath = "c:/temp/danny.csv"
Connect-SPOService -url $AdminCenterURL
Connect-AzureAD
$Sites = Get-SPOSite -Limit ALL
$Results = New-Object 'System.Collections.Generic.List[psobject]'
forEach($s in $Sites) {
If($_.Template -like 'GROUP*')
{
$GroupOwners = (Get-AzureADGroupOwner -ObjectId $s.GroupID | Select -ExpandProperty UserPrincipalName) -join "; "
}
Else{
$GroupOwners = $s.Owner
}
$SiteAdmins = (Get-SPOUser -Site $s.URL -Limit ALL | Where-Object { $_.IsSiteAdmin -eq $True} | Select-Object -ExpandProperty DisplayName) -join "; "
$Results.Add( $(New-Object PSObject -Property $([ordered]@{
'Site Title' = $s.Title
'URL' = $s.Url
'Owner(s)' = $GroupOwners
'Admin(s)' = $SiteAdmins
})))
}
$Results| Export-CSV -path $CSVPath -NoTypeInformation -Encoding utf8
Hope that helps.