Power shell script to get all external users from the SharePoint online site using PnP power shell

Loading

Import PnP PowerShell Module – Ensures that PnP PowerShell is available.

Set the SharePoint Site URL – Replace with your actual SharePoint site URL.

Connect to SharePoint Online – Uses interactive authentication.

Retrieve All Users – Uses Get-PnPUser to fetch users from the SharePoint Online site.

Filter External Users – Identifies users with #EXT# in their login name, which is a typical marker for external users.

Display Users – Formats and displays external users in a table.

Export to CSV (Optional) – Saves the results in a CSV file for further review.

Import-Module PnP.PowerShell

# Set your SharePoint Online site URL
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"

# Connect to SharePoint Online
Connect-PnPOnline -Url $siteUrl -Interactive

# Get all users from the site
$users = Get-PnPUser

# Filter external users (usually have #EXT# in their login name)
$externalUsers = $users | Where-Object { $_.LoginName -match "#EXT#" }

# Display the external users
$externalUsers | Select-Object Title, Email, LoginName | Format-Table -AutoSize

# Optional: Export to CSV
$externalUsers | Select-Object Title, Email, LoginName | Export-Csv -Path "ExternalUsers.csv" -NoTypeInformation

Write-Host "External users have been retrieved successfully."

Leave a Reply

Your email address will not be published. Required fields are marked *