Microsoft 365 : PowerShell script to get all the external users from all site collections in a 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 Tenant – Get-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
- Microsoft.Online.SharePoint.PowerShell – https://learn.microsoft.com/en-us/powershell/module/sharepoint-online/?view=sharepoint-ps
- Get-Credential – https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-7.3
- Connect-SPOService – https://learn.microsoft.com/en-us/powershell/module/sharepoint-online/connect-sposervice?view=sharepoint-ps
- Get-SPOSite – https://learn.microsoft.com/en-us/powershell/module/sharepoint-online/get-sposite?view=sharepoint-ps
- Get-SPOExternalUser – https://learn.microsoft.com/en-us/powershell/module/sharepoint-online/get-spoexternaluser?view=sharepoint-ps
- Export-Csv – Write a CSV file with PowerShell using the Export-CSV – https://knowledge-junction.in/2022/10/31/write-a-csv-file-with-powershell-using-the-export-csv-function/
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 🙂

1 Response
[…] 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… […]