So, here goes:
Remember that the UPN should always be in claims format.
The script will remove the specified UPN from all SharePoint site collections (OneDrive personal sites included).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#Parameters $TenantURL = https://tenant.sharepoint.com $UserID="i:0#.f|membership|testuser@tenant.onmicrosoft.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 -IncludeOneDriveSites #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 | ? {$_.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 } } |
Enjoy!
