@frob-0826
1.Use below PowerShell.
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
#Config Parameters
$AdminSiteURL="https://tenant -admin.sharepoint.com"
$ReportOutput ="C:\ExternalUsersRpt.csv"
#Get Credentials to connect
$Cred = Get-Credential
#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $Cred
#Get all Site Collections
$SitesCollection = Get-SPOSite -Limit ALL
$ExternalUsers=@()
#Iterate through each site collection
ForEach($Site in $SitesCollection)
{
Write-host -f Yellow "Checking Site Collection:"$Site.URL
#Get All External users of the site collection
$ExtUsers = Get-SPOUser -Limit All –Site $Site.URL | Where {$_.LoginName -like "*#ext#*" -or $_.LoginName -like "*urn:spo:guest*"}
If($ExtUsers.count -gt 0)
{
Write-host -f Green "Found $($ExtUsers.count) External User(s)!"
$ExternalUsers += $ExtUsers
}
}
#Export the Data to CSV file
$ExternalUsers | Export-Csv -Path $ReportOutput –NoTypeInformation
2.Use below PnP PowerShell.
#Parameter
$Domain = "tenant" #Domain Name in SharePoint Online. E.g. https://tenant.SharePoint.com
$CSVFile = "C:\ExternalSharing.csv"
#Frame Tenant URL and Tenant Admin URL
$TenantURL = "https://$Domain.SharePoint.com"
$TenantAdminURL = "https://$Domain-Admin.SharePoint.com"
#Delete the Output report file if exists
If (Test-Path $CSVFile) { Remove-Item $CSVFile }
#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -UseWebLogin
#Get All Site collections with External sharing enabled - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL'" | Where {$_.SharingCapability -ne "Disabled"}
#Iterate through all site collections
$Sites | ForEach-Object {
Write-host "Getting External Users of Site:"$_.URL -f Yellow
#Connect to each site collection
$SiteConn = Connect-PnPOnline -Url $_.URL -UseWebLogin -ReturnConnection
$ExternalUsersData = @()
#Get all External Users of the site collection
$ExternalUsers = Get-PnPUser -Connection $SiteConn| Where {$_.LoginName -like "*#ext#*" -or $_.LoginName -like "*urn:spo:guest*"}
Write-host "`tFound '$($ExternalUsers.count)' External users" -f Green
#Collect Data
ForEach($User in $ExternalUsers)
{
$ExternalUsersData += New-Object PSObject -Property ([ordered]@{
SiteName = $_.Title
SiteURL = $_.URL
UserName = $User.Title
Email = $User.Email
})
}
#Export Documents Inventory to CSV
$ExternalUsersData | Export-CSV $CSVFile -NoTypeInformation -Append
Disconnect-PnPOnline -Connection $SiteConn
}
Write-host "External Users Report Generated Successfully!" -f Magenta
If an Answer is helpful, please click "Accept Answer" and upvote it
Note: Please follow the steps in our email-notifications.htmldocumentation to enable e-mail notifications if you want to receive the related email notification for this thread.