Skip to content

Instantly share code, notes, and snippets.

@junecastillote
Created August 18, 2023 18:00
Show Gist options
  • Save junecastillote/db058de5afd17f46e15b607ba2c24ffe to your computer and use it in GitHub Desktop.
Save junecastillote/db058de5afd17f46e15b607ba2c24ffe to your computer and use it in GitHub Desktop.
Get All External Users on All SPO Sites
#Get_SPO_External_Users_All.ps1
# Get SharePoint Online Sites
$site = Get-SPOSite -Limit All
# Initialize the results placeholder arraylist
$externalUsers = [System.Collections.ArrayList]@()
for ($i = 0; $i -lt ($site.Count); $i++) {
# Set the initial position index to 0 (zero-based index).
$positionIndex = 0
# Execute the do-while loop to get all external SharePoint users exceeding 50.
do {
# Get the SharePoint Online external users from $positionIndex, maximun of 50.
$temp = @(Get-SPOExternalUser -SiteUrl $site[$i].Url -PageSize 50 -Position $positionIndex)
# If the last retrieved external users count is not 0, add it to the $externalUsers collection.
if ($temp.Count -gt 0 ) {
$externalUsers.AddRange(
@(
$temp | Select-Object `
@{n = 'UniqueId'; e = { $_.UniqueId } },
@{n = 'Name'; e = { $_.DisplayName } },
@{n = 'Email'; e = { $_.Email } },
@{n = 'AcceptedAs'; e = { $_.AcceptedAs } },
@{n = 'Created'; e = { $_.WhenCreated } },
@{n = 'SiteName'; e = { $site[$i].Title } },
@{n = 'SiteURL'; e = { $site[$i].Url } }
)
)
}
# Increment the position index by 50, the maximum page size
$positionIndex += 50
}
while (
# Continue the loop if the last retrieved external users count is 50
$temp.Count -eq 50
# Otherwise, exit the loop.
)
}
# Display the results
return $externalUsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment