Microsoft 365 – PowerShell – How to get all externally shared sites in our tenant

Hi All,
Greetings for the day!!!
Today small script but useful 🙂
Use Case / Background
- In our tenant, we need to find that – for given external user how many sites have access
- As such we do not have direct option to check the permission of given external users directly on all the sites
- We have an option to iterate through all the sites and get external users using – Get-SPOExternalUser
- 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-users-from-all-site-collections-in-a-tenant/
- Since we have huge number of site collection (around 100000) the scripts run bit slow and takes time
- So here using bit different approach – first only getting external sites, export to CSV and then traverse through externally shared sites and get external users
- In this article I’ll discuss step 1 – how to get all externally shared sites in our M365 tenant and export to CSV file
Details / Steps to get all externally shared sites in Tenant
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 of externally shared sites
#Configuring required parameters
#SharePoint admin site URLs
$adminSiteURL="https://knowledgejunction1-admin.sharepoint.com"
#result CSV file path
$rsultFilePath ="C:\Articles\PowerShell\ExternalSharedSites\ExternalSitesInTenant.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 externally shared site collections in Tenant – Get-SPOSite and using SharingCapability attribute / property and export to respective CSV file
#Get All Site Collections in Tenant
Get-SPOSite -Limit All -IncludePersonalSite $false Where {$_.SharingCapability -ne "Disabled"} | Select URL >> c:\temp\knowledgejunctionexternalsites.csv
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>]

- As in above code, We are checking if value for “SharingCapability” property is not disabled. This means respective site is externally shared.
- Once above script executed successfully, we have an all externally shared sites listed in CSV file as

REFERENCES
Microsoft 365 : PowerShell script to get all the external users from all site collections in a tenant – https://knowledge-junction.in/2023/01/20/microsoft-365-powershell-script-to-get-all-the-external-users-from-all-site-collections-in-a-tenant/
Thanks for reading!! HAVE a FANTASTIC LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂
You must log in to post a comment.