Get All the Users for All the Site Collections in a SharePoint Online Tenant

The next script allows to get the users from the SharePoint Online site collection / sites using PowerShell script. The SPO Management shell has API to get all the users from all the sites in the SharePoint online.

 

#################################################################
# 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
 } 
 Write-Host "Getting users" -ForegroundColor Green 
 ##Get-SPOUser -Site "<SITECOLLECTION URL HERE>" | Out-File "C:UsersSenthamilDownloadsSharePoint OnlineUsers.txt" -Append;
 }
 catch [System.Exception]
 {
 write-host -f red $_.Exception.ToString() 
 } 
}

#Connection to Office 365
$sUserName="UID@domain.onmicrosoft.com"
$sMessage="SPO Credential Please"
$sSPOAdminCenterUrl="https://<DOMAIN>-admin.sharepoint.com/"

Get-SPOUsersAllSiteCollections -sUserName $sUserName -sMessage $sMessage
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s