Hello there,
Parameters
$TenantURL = "https:// xxxx.sharepoint. com"
$UserID="i:0#.f|membership|user@X . com"
Get Credentials to connect
$Credential = Get-Credential
Frame Tenant Admin URL from Tenant URL
$TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")
Connect to PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
Get All Site collections - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL'"
Iterate through all sites
$Sites | ForEach-Object {
Write-host "Searching in Site Collection:"$.URL -f Yellow
#Connect to each site collection
Connect-PnPOnline -Url $.URL -Credentials $Credential
If((Get-PnPUser | Where {$.LoginName -eq $UserID}) -ne $NULL)
{
#Remove user from site collection
Remove-PnPUser -Identity $UserID -Confirm:$false
Write-host "`tRemoved the User from Site:"$.URL -f Green
}
}
This PnP PowerShell removes the user from all SharePoint Online sites in the tenant.
--If the reply is helpful, please Upvote and Accept it as an answer–