Sharepoint Online PowerShell script to enumerate all sites and subsites and users
-
22-01-2021 - |
Question
I have this task to iterate through a tenant, listing each site and subsite and its users (if possible with permissions). I've seen a script that says to do that but it does not list the subsites and the users that are there. The script that I am referring is :
Script that allows to get all the users for all the Site Collections in a SharePoint Online Tenant Required Parameters:
-> $sUserName: User Name to connect to the SharePoint Admin Center.
-> $sMessage: Message to show in the user credentials prompt.
-> $sSPOAdminCenterUrl: SharePoint Admin Center Url
$host.Runspace.ThreadOptions = "ReuseThread"
Definition of the function that gets all the site collections information in a SharePoint Online tenant
function Get-SPOUsersAllSiteCollections
{
param ($sUserName,$sMessage)
try
{
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Getting the information for all the site colletions in the Office 365 tenant" -foregroundcolor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
#$msolcred = get-credential -UserName $sUserName -Message $sMessage
#Connect-SPOService -Url $sSPOAdminCenterUrl -Credential $msolcred
$spoSites=Get-SPOSite | Select *
foreach($spoSite in $spoSites)
{
Write-Host "Users for " $spoSite.Url -foregroundcolor Blue
Get-SPOUser -Site $spoSite.Url
Write-Host
}
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}
#Connection to Office 365
$sUserName="<YourOffice365Account>"
$sMessage="Introduce your SPO Credentials"
$sSPOAdminCenterUrl="https://<YourDomain>-admin.sharepoint.com/"
Get-SPOUsersAllSiteCollections -sUserName $sUserName -sMessage $sMessage
the link is : https://github.com/CompartiMOSS/SharePoint-PowerShell/blob/master/Office%20365/PS_Get_SPOUsers_AllSiteCollections.ps1
What I'm referring to 'each subsite' is :
I may have a site ('Joe') like this:
https://contoso.sharepoint.com/sites/Joe/SitePages/Home.aspx
and a subsite, under 'Joe' like this:
https://contoso.sharepoint.com/sites/Joe/SubSiteJoe/SitePages/Home.aspx
The script shows the 'Joe' site and the users, but not the 'SubSiteJoe' under it.
Any ideas if this is possible ?
Solution
I've been experimenting with ShareGate Permission Matrix report in O365 v8.3.4 and I have to say its buggy and untrustworthy. It's better to use a powershell script!
OTHER TIPS
If you have a ShareGate licens inhouse this sounds like the Permission Matrix report. I guess once you have this report you will have rearrange permissions, move sites and so on. In that case ih really pays off to look at one of the available tools , like ShareGate or Metalogix. ( I don't get kickbacks or anything like it, I just like NOT to reinvent the wheel)