Microsoft 365 : PowerShell script to get all the external users from all site collections in a tenant

PowerShell script to list all site collections in Microsoft 365 tenant
PowerShell script to list all site collections in Microsoft 365 tenant

Hi All,

Greetings for the day!!

Today one more important PowerShell script.

Background / Details

  • In our organization, our security team want to know list of external users with personal accounts (like – gmail / hotmail / outlook and so on) from our tenant from all SharePoint site collections
  • We have written PowerShell script to get all the external users from all SharePoint sites in a tenant and then filtering on personal accounts
  • So sharing here small script but seems to be very useful

Steps

Step 1 : Import SharePoint Online PowerShell module – Import-Module

#Import module SharePoint Online PowerShell module
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

Step 2 : Set the required parameters

  • SharePoint admin site URL
  • Path to CSV file where we need to export our CSV file
#Configuring required parameters

#SharePoint admin site URLs
$adminSiteURL="https://knoweldgejunction1-admin.sharepoint.com"

#result CSV file path
$rsultFilePath ="C:\Articles\PowerShell\ExternalUsers\ExternalUsersInTenant.csv"

Step 3 : Get the credential to connect our TenantGet-Credential

#Get Credentials to connect to our tenant
$Cred = Get-Credential
Syntax

Get-Credential
   [[-Credential] <PSCredential>]
   [<CommonParameters>]


Get-Credential
   [-Message <String>]
   [[-UserName] <String>]
   [-Title <String>]
   [<CommonParameters>]

Step 4 : Create a SharePoint online connection – Connect-SPOService

#Connects a SharePoint Online administrator or Global Administrator to a SharePoint Online connection (the SharePoint Online Administration Center)

Connect-SPOService -URL $AdminSiteURL -Credential $Cred

Syntax 

Connect-SPOService
       -AuthenticationUrl <String>
       [-ClientTag <String>]
       [-Credential <CredentialCmdletPipeBind>]
       -Url <UrlCmdletPipeBind>
       -ModernAuth <Boolean>
       [<CommonParameters>]

Connect-SPOService
       [-ClientTag <String>]
       [-Credential <CredentialCmdletPipeBind>]
       [-Region <AADCrossTenantAuthenticationLocation>]
       -Url <UrlCmdletPipeBind>
       [<CommonParameters>]

Step 5 : Get all site collections in Tenant – Get-SPOSite

#Get All Site Collections in Tenant
$SiteCollections  = Get-SPOSite -Limit All

Syntax

Get-SPOSite
   [[-Identity] <SpoSitePipeBind>]
   [-Detailed]
   [-Limit <String>]
   [<CommonParameters>]

Get-SPOSite
   [-Detailed]
   [-Filter <String>]
   [-IncludePersonalSite <Boolean>]
   [-Limit <String>]
   [-Template <String>]
   [-GroupIdDefined]
   [<CommonParameters>]

Get-SPOSite
   [-Identity] <SpoSitePipeBind>
   [-DisableSharingForNonOwnersStatus]
   [<CommonParameters>]

Step 6 : Loop through each site and fetch the external users – Get-SPOExternalUser

#Loop through each site collection and get external users
Foreach ($Site in $SiteCollections)
{
    Write-host -f green "Site Collection:"$Site.URL
    Try {
            $externalUsers += Get-SPOExternalUser -SiteUrl $Site.Url -Position $0 -PageSize 50 | Select DisplayName,EMail,InvitedBy,AcceptedAs,WhenCreated,@{Name = "SiteUrl" ; Expression = {$Site.url}}

}#try


# do your exception handling
catch {}
}#foreach
Syntax

Get-SPOExternalUser
   [[-Position] <Int32>]
   [[-PageSize] <Int32>]
   [[-Filter] <String>]
   [[-SortOrder] <SortOrder>]
   [[-SiteUrl] <String>]
   [-ShowOnlyUsersWithAcceptingAccountNotMatchInvitedAccount <Boolean>]
   [<CommonParameters>]

Step 7 : Export the result in CSV file – Export-Csv

#Export the result to CSV file
$externalUsers | Export-Csv -Path $rsultFilePath -NoTypeInformation

COMPLETE SCRIPT

#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
 
#Configuring required parameters

#SharePoint admin site URLs
$adminSiteURL="https://knoweldgejunction1-admin.sharepoint.com"
#result CSV file path
$rsultFilePath ="C:\Articles\PowerShell\ExternalUsers\ExternalUsersInTenant.csv"
 
#Get Credentials to connect to our tenant
$Cred = Get-Credential
 
#Connects a SharePoint Online administrator or Global Administrator to a SharePoint Online connection (the SharePoint Online Administration Center)
Connect-SPOService -URL $AdminSiteURL -Credential $Cred
 
#Get All Site Collections
$SiteCollections  = Get-SPOSite -Limit All
 
#Loop through each site collection and get external users
Foreach ($Site in $SiteCollections)
{
    Write-host -f green "Site Collection:"$Site.URL
    Try {
            $externalUsers += Get-SPOExternalUser -SiteUrl $Site.Url -Position $0 -PageSize 50 -ErrorAction Stop | Select DisplayName,EMail,InvitedBy,AcceptedAs,WhenCreated,@{Name = "SiteUrl" ; Expression = {$Site.url}}
}#try

catch {}

}#foreach
 
#Export the result to CSV file
$externalUsers | Export-Csv -Path $rsultFilePath -NoTypeInformation

REFERENCES

Thanks for reading the article !! Please feel free to discuss in case any issues / suggestions / thoughts / questions !

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

1 Response

  1. August 16, 2023

    […] We already have a detailed article to get all external users from all site collection in a tenant – https://knowledge-junction.in/2023/01/20/microsoft-365-powershell-script-to-get-all-the-external-use&#8230; […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading