kwizcom banner advertisement

SharePoint Online: How to Remove External Users using PowerShell?

How to Remove External User from SharePoint Online?
To remove an external user from SharePoint Online, we've to delete them from both site collection permissions and at Office 365 tenant levels.

Step 1: Remove External User from SharePoint Online Tenant
Use Remove-SPOExternalUser cmdlet in SharePoint Online to remove external user using PowerShell. To use the Remove-SPOExternalUser cmdlet, you'll need the unique ID of the external user
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

#Config Parameters
$AdminSiteURL="https://crescenttech-admin.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $Cred

$ExternalUserID = "salaudeenar@hotmail.com"
#Get the ID of the External User
$ExternalUser = Get-SPOExternalUser -SiteUrl -filter $ExternalUserID

#Remove External User  
Remove-SPOExternalUser -UniqueIDs @($ExternalUser.UniqueId) -Confirm:$falses
This removes the external user from SharePoint Online tenant. However, it leaves "User Information List" intact. So, We'll have to use Get-SPOUser and Remove-SPOUser cmdlets to completely delete the external user.

Step 2: Delete External user from SharePoint Online Site Collection:
To remove external user in SharePoint Online site collection, follow these steps:
  • Go to Site Settings >> People and Groups. Now the URL should look like: https://YourCompany.sharepoint.com/_layouts/15/people.aspx?MembershipGroupId=XX. Change the XX to "0" and hit Enter (https://YourCompany.sharepoint.com/_layouts/15/people.aspx?MembershipGroupId=0
  • Select the Check box next to the user and click Actions >> Delete Users from Site Collection
  • Confirm the prompt once to remove the external user from SharePoint Online.
    remove external user from sharepoint online

This removes the external user from the particular site collection. This step can be done with below PowerShell as well.

Delete External user from SharePoint Online Site Collection using PowerShell
After connecting to SharePoint Online with PowerShell, run this cmdlet to get all external user of the site collection, lets get external users in SharePoint Site Collection:
$SiteUrl = "https://crescent.sharepoint.com"
Get-SPOUser -Limit All –Site $SiteURL | Where {$_.LoginName -like "*#ext#*" -or $_.LoginName -like "*urn:spo:guest*"}
Make a note of the User IDs returned.

Remove External User using PowerShell
Get the external user's Id listed above and run the Remove-SPOUser cmdlet to remove the external user from SharePoint Online site collection.
$ExternalUserID= "salaudeen_hotmail.com#ext#@crescent.com"
Remove-SPOUser -Site $SiteURL –LoginName $ExternalUserID


PowerShell to Delete All External Users in SharePoint Online:
How about deleting all external users in all site collections after you disabled external sharing at tenant level? Well, In order to remove an external user from SharePoint Online, we've to delete them from both site collection permissions and Office 365 tenant levels.
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $Cred

#Get all Site Collections
$SitesCollection = Get-SPOSite -Limit ALL

#Iterate through each site collection
ForEach($Site in $SitesCollection)
{
    Write-host -f Yellow "Checking Site Collection:"$Site.URL

    #Get All External users of the site collection
    $ExternalUsers = Get-SPOUser -Limit All –Site $Site.URL | Where {$_.LoginName -like "*#ext#*" -or $_.LoginName -like "*urn:spo:guest*"}

    #Loop through each User and remove them from site collection
    ForEach($ExtUser in $ExternalUsers)
    {
        #Remove the user from the site collection
        Remove-SPOUser -Site $Site.URL –LoginName $ExtUser.LoginName
        Write-host -f Green "External User $($ExtUser.LoginName) has been removed from site collection" 
    }
}
    #Remove All External Users at Tenenat Level
    $TenantExternalUsers=@()
    #Get All External Users at Tenant Level
    Try {
        For ($x=0;;$x+=50) {
            $TenantExternalUsers += Get-SPOExternalUser -Position $x -PageSize 50 -ErrorAction Stop
        }
    }
    catch {}

    $TenantExternalUsers | ForEach-Object {
    Remove-SPOExternalUser @($_.UniqueId) -Confirm:$false
    Write-host -f Green "External User $($ExtUser.LoginName) has been removed from the tenant!"
    }
Please note: You may have to clear browser cache, in case you could still find the removed external users in places like People Picker!
SharePoint Online: How to Remove External Users using PowerShell? SharePoint Online: How to Remove External Users using PowerShell? Reviewed by Salaudeen Rajack on September 13, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.