To retrieve all users in a SharePoint Online site collection using PowerShell, you can use the SharePoint Online Management Shell module. Here's a PowerShell script to accomplish this:
# Define the SharePoint Online site collection URL
$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSiteCollection" # Replace with your site collection URL
# Connect to SharePoint Online
Connect-SPOService -Url "https://yourtenant-admin.sharepoint.com"
# Connect to the SharePoint site collection
Connect-PnPOnline -Url $siteUrl -UseWebLogin
# Get all users in the site collection
$users = Get-PnPTenantSite | ForEach-Object { Get-PnPTenantSiteTemplate $_.TemplateId } | Where-Object { $_.Type -eq "List" } | Get-PnPTenantList | ForEach-Object {
$list = $_
Get-PnPTenantUser -List $list
}
# Display user information
foreach ($user in $users) {
Write-Host "User Display Name: $($user.DisplayName)"
Write-Host "User Email: $($user.Email)"
Write-Host "User Login Name: $($user.LoginName)"
Write-Host "------------------------"
}
# Disconnect from SharePoint Online
Disconnect-PnPOnline
# Disconnect from SharePoint Online Management Shell
Disconnect-SPOService
Instructions:
In this script:
- Define the SharePoint Online site collection URL ($siteUrl) for which you want to retrieve all users. Replace "https://yourtenant.sharepoint.com/sites/YourSiteCollection" with the actual site collection URL.
- Connect to SharePoint Online using Connect-SPOService and authenticate with administrative credentials.
- Connect to the specified site collection using Connect-PnPOnline and the provided URL.
- Use Get-PnPTenantUser to retrieve all users in the site collection.
- Display user information, such as display name, email, and login name, for each user.
- Disconnect from SharePoint Online using Disconnect-PnPOnline and disconnect from SharePoint Online Management Shell using Disconnect-SPOService when you're done.
After updating the script with your information, save it as a .ps1 file and run it using PowerShell. This script will retrieve and display information about all users in the specified SharePoint Online site collection.