Office 365 - Identify External Users

Here is a quick script that can be used to identify external users that have access to each Site Collection within an Office 365 tenant (excluding OneDrive for Business Sites). This script requires the SharePoint Online Management Shell which can be downloaded from - https://www.microsoft.com/en-us/download/details.aspx?id=35588. Output will be written to the console and a CSV file ($Output). Simply update the highlighted variables and execute!

#Specify tenant admin and URL
$User = "user@tenant.onmicrosoft.com"
$TenantURL = "https://tenant-admin.sharepoint.com"

#O365 Cmdlets to connect to a tenant
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking -ErrorAction SilentlyContinue
Connect-SPOService -Url $TenantURL -credential $User

$Output = "D:\Output.CSV"
"Site URL"+","+"External User" | Out-File -Encoding Default -FilePath $Output

#Check Sites
Write-Host "Checking Sharepoint Online sites for External Users" -ForegroundColor Green
$Sites = Get-SPOSite | Where {$_.SharingCapability -ne "Disabled"}
Foreach ($Site in $Sites)
{
Write-Host Checking $Site.URL -ForegroundColor Yellow
ForEach ($User in (Get-SPOExternalUser -SiteUrl $Site.URL))
{
Write-Host "-" $User.Email "Found!" -ForegroundColor White
$Site.URL + "," + $User.Email | Out-File -Encoding Default -Append -FilePath $Output
}
}

Brendan Griffin - @brendankarl